Timer question.

G

Guest

Hi,

Currently I'm using System.Threading.Timer to perform some tasks
periodically,
lets say every minute.

How can we disable this timer(System.Threading.Timer) so that the time spent
in the callback method is not counted.

Do we have an equivalent of
System.Timers.Time.Enabled
property in System.Threading.Timer class?

Kindly let me know.

Cheers,
Naveen.
 
W

wbekker

Hi Naveen,

To pauze the timer:

timer.Change(System.Threading.Timeout.Infinite,
System.Threading.Timeout.Infinite)

You need to change it back to the original interval to start the timer
again.

Ward
 
G

Guest

Hi bob,

but later on, I need to port the code to Compact Framework.
I guess System.Timers.Timer is not supported in Compact Framework.

Cheers,
Naveen.
 
J

J.Marsch

One thing that you could do would be to set a flag when you enter your timer
handler. You will ignore any timer events while the flag is set, and reset
it when you finish your handler:
example:

// warning, I'm writing this "off the cuff", so there might be syntax errors
private bool TimerHandlerInProgress;
public void MyTimerCallback(object state)
{
lock(this)
{
if(this.TimerHandlerInProgress)
return; // ignore the timer elapse-- we are still handling the
last one
else
this.TimerHandlerInProgress = true;
}

try
{
// process your timer event here....
}
finally
{
// reset the handled flag
lock(this)
{
this.TimerHandlerInProgress = false;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top