Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timer Scheduling

Status
Not open for further replies.

kettch

Programmer
Mar 5, 2001
110
US
I have a project that needs a certain set of tasks run at a specific time. I know how to use System.Timers and all that, but I was wondering if there was a better method to nail a timer to a specific time.

Right now the windows service starts out by checking the time every hour until its designated start hour and then it resets the timer.interval to be the main cycle length.

I would like to check every minute until the next zero minute. Then check every hour until the hour I need, but how resource expensive is that? I can do it this way but is there a more straight forward way to do it?

-Thanks
 
using System;
using System.Timers;
using System.Threading;

namespace AAA
{
public class MyClass
{
static int INTERVAL = 5000;
static System.Timers.Timer aTimer;

public static void Handle ()
{
aTimer = new System.Timers.Timer();
try
{
aTimer.Elapsed+=new ElapsedEventHandler(DoSomething);
aTimer.Interval=INTERVAL;
aTimer.Enabled=true;
}
catch (System.Exception e)
{
Console.WriteLine ("Error :"+e);
}
}

public static void DoSomething (object source, ElapsedEventArgs e)
{
//
// PUT YOUR CODE HERE
//
}
}
}

" ahhh computers, how they made our lives much simpler ;) "
 
this code is ready to go and it is what I've been using for a timer, you can add in some code for the INTERVAL logic to do what you need


" ahhh computers, how they made our lives much simpler ;) "
 
Thanks for the help, but I have been fiddling around and I came up with some code that will be less expensive resource-wise. I'll post it as soon as I get it perfected. It does some hairy calculations to figure out what time is the time to start i.e 2100 hours.

Then it sets that TimeSpan.Ticks to be the interval for the timer. That way the timer will not be throwing events until I want it too. Then when it comes to the proper time, I can reset the interval to be the main cycle length.

I don't know how resource intensive checking all of the time is, but the server I'm on also runs some mission critical stuff so I am being paranoid.

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top