tags:

views:

93

answers:

3

The point of this timer is to execute some code whenever it's midnight. So basically, the first interval will be between Now and midnight and all intervals following that will be 24hours.

Now that's all well and good, but I was wondering how this timer business works. Is the MyTimer.Interval recalculated each time the timer resets?

System.Timers.Timer MyTimer = new System.Timers.Timer();
MyTimer.Elapsed += new ElapsedEventHandler(TriggeredMethod);
MyTimer.Interval = [pseudocode]Time between now and midnight[/pseudocode];
MyTimer.Start();

EDIT:

I'm having trouble setting the Interval inside of my TriggerMethod. Where/how should I initiate the Timer so I don't get any context errors?

private void Form1_Load(object sender, EventArgs e)
{
System.Timers.Timer MyTimer = new System.Timers.Timer();
MyTimer.Elapsed += new ElapsedEventHandler(TriggeredMethod);
MyTimer.Start();
}

private void TriggerMethod(object source, ElapsedEventArgs e)
{
MyTimer.Interval = [pseudocode]Time between now and midnight[/pseudocode];
}
+1  A: 

Hey,

after the first run, you would have to update the interval, subtracting the current time from midnight the following day and assigning to the interval; I do this for one of my batch processes, and it works well.

HTH.

Brian
@Brian How can I make this change of interval after the first run? I don't know how I would determine if the first run has completed or not...
Soo
You should set `MyTimer.Interval` in `TriggeredMethod`.
SLaks
@SLaks, hey, I'm confused about the context of the Timer object. I made an edit to my original post, if you could help me out, I'd really appreciate it!
Soo
At the end of the process, you recalculate again...
Brian
+1  A: 

The Interval property is the number of milliseconds between timer invocations.

To run the timer at midnight, you would need to change Interval in each Elapsed event to (int)(DateTime.Today.AddDays(1) - DateTime.Now).TotalMilliseconds.

To access the timer inside the Elapsed handler, you'll need to store the timer in a field in your class, like this:

System.Timers.Timer MyTimer
private void Form1_Load(object sender, EventArgs e)
{
    MyTimer = new System.Timers.Timer();
    //...
}

Note, by the way, that System.Timers.Timer will not fire on the UI thread.
Therefore, you cannot manipulate the form inside the Elapsed handler.
If you need to manipulate the form, you can either switch to System.Windows.Forms.Timer (which is less accurate) or call BeginInvoke.

SLaks
Can you clarify what you mean by Tick event?Does this mean the Interval needs to be reassigned inside of the method that is triggered by the timer?
Soo
Yes, it ​​does. You'll need to store `MyTimer` in a field in your class.
SLaks
Move `System.Timers.Timer MyTimer;` outside the `Form_Load` method.
SLaks
Ok, I think I finally get it. The first time interval is specified in the Form_Load method, and each time interval after that is specified inside of the TriggeredMethod.THANKS! :D
Soo
A: 

Make your timer fire once per hour, and then only do the actual work at midnight. And I also recommend using BeginInvoke to move the actual UI interaction onto the GUI thread.

jsight