• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 543
  • Last Modified:

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);
      }
      
0
shirlng
Asked:
shirlng
  • 4
  • 2
  • 2
1 Solution
 
vinhnlCommented:
Please use timer class:

private System.Windows.Forms.Timer timer1;
private DateTime LastDate = null;

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 == null || LastDate.Day != DateTime.Now.Day ))
                {
                       SendMail();
                       LastDate = DateTime.Now;
                 }
          }

Good luck
VINHNL
0
 
shirlngAuthor Commented:
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!!
0
 
rameedevCommented:
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
vinhnlCommented:
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;
                 }
          }
0
 
vinhnlCommented:

if(DateTime.Now.Hour == 8 && DateTime.Now.Minute == 0 && LastDate.Day != DateTime.Now.Day )
0
 
shirlngAuthor Commented:
What if I use sleep threading for this? will it be a better alternative?
Thanks!
0
 
vinhnlCommented:
Oh no. don't use sleep threading. It will appropriate the resource of the proccess
0
 
rameedevCommented:
Have you tried whatever possible solution that was posted...Bcoz i dont see a compelling reason to use threads here

Rameedev
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now