Link to home
Start Free TrialLog in
Avatar of shirlng
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);
      }
      
ASKER CERTIFIED SOLUTION
Avatar of vinhnl
vinhnl

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shirlng
shirlng

ASKER

I have the following errors:
private DateTime LastDate = null;
(C:\dotnet\CEAMail\Service1.cs(23): Cannot convert null to 'System.DateTime' because it is a value type)

if(DateTime.Now.Hour == 8 && DateTime.Now.Minute == 0 && (LastDate == null || LastDate.Day != DateTime.Now.Day ))
(C:\dotnet\CEAMail\Service1.cs(107): Operator '==' cannot be applied to operands of type 'System.DateTime' and '<null>')

Can anyone help?
Thanks!!
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);//Attach 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 System.Windows.Forms.Timer timer1;
private DateTime LastDate = new DateTime(2000,1,1);

private void InitializeComponent()
          {
               this.timer1 = new System.Windows.Forms.Timer(this.components);
               //
               // timer1
               //
               this.timer1.Enabled = true;
               this.timer1.Interval = 1000;
               this.timer1.Tick += new System.EventHandler(this.timer1_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;
                 }
          }

if(DateTime.Now.Hour == 8 && DateTime.Now.Minute == 0 && LastDate.Day != DateTime.Now.Day )
Avatar of shirlng

ASKER

What if I use sleep threading for this? will it be a better alternative?
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