![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2005
Posts: 18
Rep Power: 0
![]() |
Calling a function periodically
Hello, I need to call a function periodically, and currently I'm using the code:
TimerCallback callBack = new TimerCallback(OnTick); m_Timer = new System.Threading.Timer(callBack, null, 0, 2500); (Where OnTick is the function to be called periodically, and 2500 is the number of milliseconds to wait in between calls--any number would do.) This works fine, except that this timer calls OnTick every 2.5 seconds, regardless of whether the previously called instance of OnTick has or has not yet finished its execution. (This causes problems for my particular application.) What I'm looking for is some code which does the above, except that it calls OnTick 2.5 seconds AFTER the LAST OnTick instance called finishes, or at least if a current instance of OnTick is running, it doesn't call a second one. Thank you for your time! |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Use the dispose or change method to make it a one-shot timer, then fire a new one off at the end of the timer service function.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: May 2005
Location: ma
Posts: 130
Rep Power: 4
![]() |
i don't know if this would work plus i've been drinking so take this with a grain of salt but could you put a variable in the OnTick that would be called something like running or inprogress and set it to zero outside of the function but when the function is called it sets it to one and when it is done sets it back to zero and have it so the function will only start if inprogress (or whatever you call it) is set to zero. good thing it is impossible to slurr by typing. good luck.
|
|
|
|
|
|
#4 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
Kick off a thread for the function you want called periodically. Change the function so that it loops and at the end of the loop there is:
System.Threading.Thread.CurrentThread.Sleep(2500); And there you have it. It just pauses 2.5 seconds between each iteration. Instead of modifying the target function you could have a seperate function with the loop and pausing code that calls your desired function. That might be a little cleaner.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Apr 2005
Posts: 18
Rep Power: 0
![]() |
Thank you all for your replies--I tried DaWei's suggestion first and this seems to work satisfactorially. I was somewhat hoping that to find some function built into the C# language which could do this elegantly, but I guess no such function exists!
Thanks again! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|