Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 help with timer class

Author  Topic 

-Dman100-
Posting Yak Master

210 Posts

Posted - 2008-11-10 : 21:39:36
I am trying to understand how to use the timer class so I can call and execute a windows service program every night at 1:00AM.

I've read several articles on using the timer class and I am still a little confused on how to correctly use this class.

I want the timer to execute my program every night at 1:00AM (see code below)


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using SforceIntegration;
using System.Timers;

namespace SforcePrototypeService

{

public partial class Service1 : ServiceBase

{

Timer timer = new Timer();

public Service1()

{

InitializeComponent();

}

private void OnElapsedTime(object source, ElapsedEventArgs e)

{

Library lib = new Library();

lib.Start();

}

protected override void OnStart(string[] args)

{

timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);

timer.Interval = 60000;

timer.Enabled = true;


}

protected override void OnStop()

{

timer.Enabled = false;

}

}

}

The way the timer is working currently is running the program every minute. How can I switch this to run every night at 1:00AM?

Thanks for any help.

cvraghu
Posting Yak Master

187 Posts

Posted - 2008-11-11 : 02:53:43
Once you start the windows service and your job is done are you stopping it too? Otherwise it might be running and consuming memory. Its not good for one-time job.

Better approach would be to write an application and schedule it using windows scheduler.
Go to Top of Page

-Dman100-
Posting Yak Master

210 Posts

Posted - 2008-11-11 : 08:39:51
The job needs to run nightly. So, it isn't a one-time job. The requirements is for this to run as a service. Is it possible to setup the service to run every night at 1:00AM?

Thanks for the help.
Go to Top of Page

cvraghu
Posting Yak Master

187 Posts

Posted - 2008-11-12 : 00:03:12
A windows service could be started automatically during system startup or manually. It could also be started through code. What is the criteria for you to stop it, so you could start it next day? Also there should be another continuous process which does this.

If you still need a service, then build the service. Set it to startup automatically during system startup. Have the timer logic INSIDE the service. Hope this helps.
Go to Top of Page
   

- Advertisement -