shirlng
asked on
Add timer to run the window services once a day
I have this window services and I want to put a timer for the following email sending routine. It should only run once a day at 8am. How should I do it? Thanks!
Thanks in advance for all your help.
string sEmail = "";
{
while (dr.Read())
sEmail = dr["email"].ToString();
if (sEmail != "")
{
MailMessage m = new MailMessage();
SmtpMail.SmtpServer="smtp. abcde.com" ;
m.To= sEmail;
m.From = "CEA@abcde.com";
m.Subject= dr["subject"].ToString();
m.Body= dr["content"].ToString() + "\n\n Pending approval = " + dr["waiting"].ToString();
SmtpMail.Send(m);
}
Thanks in advance for all your help.
string sEmail = "";
{
while (dr.Read())
sEmail = dr["email"].ToString();
if (sEmail != "")
{
MailMessage m = new MailMessage();
SmtpMail.SmtpServer="smtp.
m.To= sEmail;
m.From = "CEA@abcde.com";
m.Subject= dr["subject"].ToString();
m.Body= dr["content"].ToString() + "\n\n Pending approval = " + dr["waiting"].ToString();
SmtpMail.Send(m);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
using System.Timers;
private Timer m_SerTimer;
public Service1()
{
InitializeComponent();
TimeSpan ts = new TimeSpan(0,0,1);
double d = ts.TotalMilliseconds;
m_SerTimer = new Timer(d);
m_SerTimer.Elapsed += new ElapsedEventHandler(timer1 _Tick);//A ttach an event handler on timer elapse
}
private void HandleTimerElapse(object sender,ElapsedEventArgs e)
{ if((DateTime.Now.Hour == 8) && (DateTime.Now.Minute == 0))
{
try
{
SendMail();
}
catch(Exception ex)
{
}
}
u dont have to set any Lastdate..bcoz ur service's time interval is for every second.On Every Second it will trigger this event and call ur SendMail...But u need to make sure to Disable the Timer first in the Handler and Enable it in the finally block
RameeDev
private Timer m_SerTimer;
public Service1()
{
InitializeComponent();
TimeSpan ts = new TimeSpan(0,0,1);
double d = ts.TotalMilliseconds;
m_SerTimer = new Timer(d);
m_SerTimer.Elapsed += new ElapsedEventHandler(timer1
}
private void HandleTimerElapse(object sender,ElapsedEventArgs e)
{ if((DateTime.Now.Hour == 8) && (DateTime.Now.Minute == 0))
{
try
{
SendMail();
}
catch(Exception ex)
{
}
}
u dont have to set any Lastdate..bcoz ur service's time interval is for every second.On Every Second it will trigger this event and call ur SendMail...But u need to make sure to Disable the Timer first in the Handler and Enable it in the finally block
RameeDev
private System.Windows.Forms.Timer timer1;
private DateTime LastDate = new DateTime(2000,1,1);
private void InitializeComponent()
{
this.timer1 = new System.Windows.Forms.Timer (this.comp onents);
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.t imer1_Tick );
}
private void timer1_Tick(object sender, System.EventArgs e)
{
if(DateTime.Now.Hours == 8 && DateTime.Now.Minus == 0 && (LastDate.Day != DateTime.Now.Day ))
{
SendMail();
LastDate = DateTime.Now;
}
}
private DateTime LastDate = new DateTime(2000,1,1);
private void InitializeComponent()
{
this.timer1 = new System.Windows.Forms.Timer
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.t
}
private void timer1_Tick(object sender, System.EventArgs e)
{
if(DateTime.Now.Hours == 8 && DateTime.Now.Minus == 0 && (LastDate.Day != DateTime.Now.Day ))
{
SendMail();
LastDate = DateTime.Now;
}
}
if(DateTime.Now.Hour == 8 && DateTime.Now.Minute == 0 && LastDate.Day != DateTime.Now.Day )
ASKER
What if I use sleep threading for this? will it be a better alternative?
Thanks!
Thanks!
Oh no. don't use sleep threading. It will appropriate the resource of the proccess
Have you tried whatever possible solution that was posted...Bcoz i dont see a compelling reason to use threads here
Rameedev
Rameedev
ASKER
private DateTime LastDate = null;
(C:\dotnet\CEAMail\Service
if(DateTime.Now.Hour == 8 && DateTime.Now.Minute == 0 && (LastDate == null || LastDate.Day != DateTime.Now.Day ))
(C:\dotnet\CEAMail\Service
Can anyone help?
Thanks!!