Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 9th, 2005, 7:15 PM   #1
Mechromancer
Newbie
 
Mechromancer's Avatar
 
Join Date: Apr 2005
Posts: 18
Rep Power: 0 Mechromancer is on a distinguished road
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!
Mechromancer is offline   Reply With Quote
Old Jul 9th, 2005, 7:58 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jul 9th, 2005, 7:58 PM   #3
linuxpimp20
Hobbyist Programmer
 
Join Date: May 2005
Location: ma
Posts: 130
Rep Power: 4 linuxpimp20 is on a distinguished road
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.
linuxpimp20 is offline   Reply With Quote
Old Jul 9th, 2005, 8:33 PM   #4
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
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
Dameon is offline   Reply With Quote
Old Jul 10th, 2005, 5:07 PM   #5
Mechromancer
Newbie
 
Mechromancer's Avatar
 
Join Date: Apr 2005
Posts: 18
Rep Power: 0 Mechromancer is on a distinguished road
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!
Mechromancer is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:42 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC