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.....
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 { } } }