I have developed new windows service that sends SMS to some numbers, and put timer but I need to know how can i customize it to run only every Friday and saturday every 3 hours . my code as below
public partial class IF_CHECK_SERVICE : ServiceBase
{
SqlConnection con = new SqlConnection("my connection details");
System.Timers.Timer objTmr = new System.Timers.Timer();
string value = "GCS Interface Jobs Run Successfuly are ";
string value2 = "GCS Interface Jobs have Problem";
string SMS = "";
public IF_CHECK_SERVICE()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
objTmr.Interval = 10800000;
objTmr.Enabled = true;
objTmr.AutoReset = true;
objTmr.Elapsed += new System.Timers.ElapsedEventHandler(objTmr_Elapsed);
}
private void objTmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// TimeSpan objTime = new TimeSpan(12, 0, 0);
//if (TimeSpan.Compare(DateTime.Now.TimeOfDay, objTime) >= 0)
//{
send();
//}
}
protected override void OnStop()
{
//RBRTimer.Stop();
//RBRTimer.Enabled = false;
}
void send()
{
try
{
if (con.State == ConnectionState.Open)
con.Close();
string outErrorMessage = string.Empty;
string messageStatus = string.Empty;
System.Data.SqlClient.SqlDataAdapter ada = new System.Data.SqlClient.SqlDataAdapter("select JOB_ID , isnull(LAST_END_DATETIME,Getdate()) [Last_run],DATEDIFF(hh,isnull(LAST_END_DATETIME,getdate()),GETDATE()) DIFF from T_SCHEDULE_MASTER_USG105 where JOB_ID in ('SPA','PCO','WBR','PSIS','PSIR','WBR','KISR') order by DIFF", con);
con.Open();
DataSet ds = new DataSet();
ada.Fill(ds);
DataTable tbl = ds.Tables[0];
for (int i = 0; i < tbl.Rows.Count; i++)
{
DataRow myRow = tbl.Rows[i];
string JOB_ID = myRow["JOB_ID"].ToString();
int diff = int.Parse(myRow["DIFF"].ToString());
string last_run_time = myRow["Last_run"].ToString();
if (diff < 2)
{
value += JOB_ID + "\r\n";
// label3.Text = value;
SMS = value;
}
if (diff >= 2)
{
value2 += JOB_ID + "\r\n";
// label1.Text = value2;
SMS += "\r\n" + value2 + "Last Running Time " + last_run_time;
}
}
//Send SMS
con.Close();
}
catch { }
}
}
Life is about trusting our feelings and taking chances, losing, finding happiness, appreciating the memories, learning from the past.....
Like Sum8 has mentioned, you just need to check if the current date is of "Friday" or "Satureday" in your windows sevice's execution code. In other words, your windows service just keep running and fire the event once every 3 hours(based on your requirements)
and the actual functional code will only be executed if today is "Friday" or "Saturday".
for checking the dayInWeeks, there is built-in function in .NET framework. e.g.
=====================
var todayInWeek = DateTime.Now.Date.DayOfWeek;
if (todayInWeek == DayOfWeek.Saturday || todayInWeek == DayOfWeek.Friday)
{
// To the tasks here
} else
{
// Do nothing
}
=====================
hardtoget410
Member
114 Points
231 Posts
Windows service runs on Firday and Saturday every 3 Hours
Mar 20, 2012 12:23 PM|LINK
I have developed new windows service that sends SMS to some numbers, and put timer but I need to know how can i customize it to run only every Friday and saturday every 3 hours . my code as below
public partial class IF_CHECK_SERVICE : ServiceBase { SqlConnection con = new SqlConnection("my connection details"); System.Timers.Timer objTmr = new System.Timers.Timer(); string value = "GCS Interface Jobs Run Successfuly are "; string value2 = "GCS Interface Jobs have Problem"; string SMS = ""; public IF_CHECK_SERVICE() { InitializeComponent(); } protected override void OnStart(string[] args) { objTmr.Interval = 10800000; objTmr.Enabled = true; objTmr.AutoReset = true; objTmr.Elapsed += new System.Timers.ElapsedEventHandler(objTmr_Elapsed); } private void objTmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { // TimeSpan objTime = new TimeSpan(12, 0, 0); //if (TimeSpan.Compare(DateTime.Now.TimeOfDay, objTime) >= 0) //{ send(); //} } protected override void OnStop() { //RBRTimer.Stop(); //RBRTimer.Enabled = false; } void send() { try { if (con.State == ConnectionState.Open) con.Close(); string outErrorMessage = string.Empty; string messageStatus = string.Empty; System.Data.SqlClient.SqlDataAdapter ada = new System.Data.SqlClient.SqlDataAdapter("select JOB_ID , isnull(LAST_END_DATETIME,Getdate()) [Last_run],DATEDIFF(hh,isnull(LAST_END_DATETIME,getdate()),GETDATE()) DIFF from T_SCHEDULE_MASTER_USG105 where JOB_ID in ('SPA','PCO','WBR','PSIS','PSIR','WBR','KISR') order by DIFF", con); con.Open(); DataSet ds = new DataSet(); ada.Fill(ds); DataTable tbl = ds.Tables[0]; for (int i = 0; i < tbl.Rows.Count; i++) { DataRow myRow = tbl.Rows[i]; string JOB_ID = myRow["JOB_ID"].ToString(); int diff = int.Parse(myRow["DIFF"].ToString()); string last_run_time = myRow["Last_run"].ToString(); if (diff < 2) { value += JOB_ID + "\r\n"; // label3.Text = value; SMS = value; } if (diff >= 2) { value2 += JOB_ID + "\r\n"; // label1.Text = value2; SMS += "\r\n" + value2 + "Last Running Time " + last_run_time; } } //Send SMS con.Close(); } catch { } } }Sum8
Contributor
4141 Points
931 Posts
Re: Windows service runs on Firday and Saturday every 3 Hours
Mar 20, 2012 12:51 PM|LINK
Try this:
if (DateTime.Now.ToString("dddd") == "Friday" || DateTime.Now.ToString("dddd") == "Saturday") { // Send SMS }Sumit Pathak
------------------
ThisPost = Helped == True ? "Mark As Answer" : "Elaborate your problem in more details"
Steven Cheng...
Contributor
4199 Points
548 Posts
Microsoft
Moderator
Re: Windows service runs on Firday and Saturday every 3 Hours
Mar 23, 2012 06:45 AM|LINK
Hi hardtoget410,
Like Sum8 has mentioned, you just need to check if the current date is of "Friday" or "Satureday" in your windows sevice's execution code. In other words, your windows service just keep running and fire the event once every 3 hours(based on your requirements) and the actual functional code will only be executed if today is "Friday" or "Saturday".
for checking the dayInWeeks, there is built-in function in .NET framework. e.g.
===================== var todayInWeek = DateTime.Now.Date.DayOfWeek; if (todayInWeek == DayOfWeek.Saturday || todayInWeek == DayOfWeek.Friday) { // To the tasks here } else { // Do nothing } =====================Feedback to us
Microsoft One Code Framework