|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to pause the timer created from System.Threading.Timer classI 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 timer.Change(Timeout.Infinite, Timeout.Infinite)
will pause timer. When you want to restart, then change again with the new values. -- Show quoteWilliam Stacey [MVP] "Dhruvesh Brahmbhatt" <DhruveshBrahmbh***@discussions.microsoft.com> wrote in message news:BEB24976-924C-4CDF-9A24-8D120E2731EB@microsoft.com... | 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 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. Show quote "William Stacey [MVP]" wrote: > timer.Change(Timeout.Infinite, Timeout.Infinite) > > will pause timer. When you want to restart, then change again with the new > values. > > > -- > William Stacey [MVP] > > > "Dhruvesh Brahmbhatt" <DhruveshBrahmbh***@discussions.microsoft.com> wrote > in message news:BEB24976-924C-4CDF-9A24-8D120E2731EB@microsoft.com... > | 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 > > > This should do your job!!!
timer.Change(Timeout.Infinite, Timeout.Infinite); 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 |
|||||||||||||||||||||||