How to pause the timer created from System.Threading.Timer class

G

Guest

Hi,

I have one problem in pausing the timer which i have created from
System.Threading.Timer class.

// I can create a timer that waits one second, then invokes every second by
below code
Timer timer = new Timer(timerDelegate, s,1000, 1000);


// I can also shorten the period. Wait 10 seconds to restart the timer.
(s.tmr).Change(10000,100);

My problem is like this,
Once i specify DueTime, the TimerDelegate will be called after that much
duration.
But If i want to Pause the DueTime and restart it then how can i do it?

In the above semple after 1000 miliseconds the timer delegate will be
executed but if i will pause the program at 500 miliseconds and want to
restart the program exactly after 500 miliseconds then how can I do it?

Please reply.

Thanks,
Dhruvesh
 
W

William Stacey [MVP]

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

will pause timer. When you want to restart, then change again with the new
values.


--
William Stacey [MVP]


in message | Hi,
|
| I have one problem in pausing the timer which i have created from
| System.Threading.Timer class.
|
| // I can create a timer that waits one second, then invokes every second
by
| below code
| Timer timer = new Timer(timerDelegate, s,1000, 1000);
|
|
| // I can also shorten the period. Wait 10 seconds to restart the timer.
| (s.tmr).Change(10000,100);
|
| My problem is like this,
| Once i specify DueTime, the TimerDelegate will be called after that much
| duration.
| But If i want to Pause the DueTime and restart it then how can i do it?
|
| In the above semple after 1000 miliseconds the timer delegate will be
| executed but if i will pause the program at 500 miliseconds and want to
| restart the program exactly after 500 miliseconds then how can I do it?
|
| Please reply.
|
| Thanks,
| Dhruvesh
 
G

Guest

Thanks for reply,

But my problem is not getting resolved by this.

I have the problem as follows,
In my application, I want to create timer object with some duetime, now if
some one pauses my application. I want to pause the internal counter of
duetime exactly when my application is getting paused. So when application
will be restarted, the total time duration for which my application was up
will raise one event.
 
G

Gancy

I dont think you keen freeze the application the way you describe you
want to do. Well, you could probablly do the following. However it
might not be very accurate solution

When the time is paused/stoped find the no of mili seconds elapsed
since the timer has started. When you 'un-pause' the application,
start the timer to run following mili seconds

reamining time = total time - elapsed time

i.e. if origanlly your timer is set to run at intervals of 5000 mili
seconds and you pause/stop the timer after, say 1500 mili seconds then

remaining time = 5000 - 1500 = 3500 mili seconds.
Hope this helps
 

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