threading and timercallback
-
Please help. Here is a snippet of my code: using TD=System.Threading; TD.Timer m_tMonitor; m_tMonitor = new TD.Timer(new TD.TimerCallback(MonitorFunc), 1, 0, 60000); I am trying to monitor a schedule every minute, and if schedule is past due then I am performing some tasks. My problem is sometimes monitorfunc is not completed in 1 minute so timercallback is triggered again. Is there a way I can use something like WAIT for monitorfunc to complete what is is doing before I call it again. HELP!!!
-
Please help. Here is a snippet of my code: using TD=System.Threading; TD.Timer m_tMonitor; m_tMonitor = new TD.Timer(new TD.TimerCallback(MonitorFunc), 1, 0, 60000); I am trying to monitor a schedule every minute, and if schedule is past due then I am performing some tasks. My problem is sometimes monitorfunc is not completed in 1 minute so timercallback is triggered again. Is there a way I can use something like WAIT for monitorfunc to complete what is is doing before I call it again. HELP!!!
Hi, what do you want to happen when MonitorFunc hasn't finished in one minute: - drop one execution (i.e. ignore the new one that overlaps, so the next run is up to one minute after the current one finishes); - stop the running one and run the new one; - finish the running one, then immediately run the new one? You decide, and code accordingly! :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Hi, what do you want to happen when MonitorFunc hasn't finished in one minute: - drop one execution (i.e. ignore the new one that overlaps, so the next run is up to one minute after the current one finishes); - stop the running one and run the new one; - finish the running one, then immediately run the new one? You decide, and code accordingly! :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
Hi Luc, I am not sure if I understand you. However, I want to finish running one, and then start the new one. Basically if monitorfunc has not finished I want to pause the call if one minute is passed. Then start the call once it has finished. I hope I am making sense. Please advice.
Sameer
-
Hi Luc, I am not sure if I understand you. However, I want to finish running one, and then start the new one. Basically if monitorfunc has not finished I want to pause the call if one minute is passed. Then start the call once it has finished. I hope I am making sense. Please advice.
Sameer
OK, so if one run exceeds the allotted time period, you want the current run to terminate normally, and the new run of the function to follow immediately. This is some pseudo-code that would achieve this:
private int activationCounter=0;
void periodicFunction() {
int n=Interlocked.Increment(ref activationCounter); // increments in a safe way
if (n<=1) { // check no other execution is going on
do {
// whatever needs to be done periodically
// (while this happens, the counter could get incremented)
// when done, decrement and test the counter:
n=Interlocked.Decrement(ref activationCounter); // decrements in a safe way
} while (n!=0); // check more is required
}:)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Please help. Here is a snippet of my code: using TD=System.Threading; TD.Timer m_tMonitor; m_tMonitor = new TD.Timer(new TD.TimerCallback(MonitorFunc), 1, 0, 60000); I am trying to monitor a schedule every minute, and if schedule is past due then I am performing some tasks. My problem is sometimes monitorfunc is not completed in 1 minute so timercallback is triggered again. Is there a way I can use something like WAIT for monitorfunc to complete what is is doing before I call it again. HELP!!!
bool stop = false;
...
Thread t = new Thread(new ThreadStart(run)){IsBackground = true}.Start();
...private void run(){
while(!stop){
Thread.Sleep(60000);
MonitorFunc();
}
} -
OK, so if one run exceeds the allotted time period, you want the current run to terminate normally, and the new run of the function to follow immediately. This is some pseudo-code that would achieve this:
private int activationCounter=0;
void periodicFunction() {
int n=Interlocked.Increment(ref activationCounter); // increments in a safe way
if (n<=1) { // check no other execution is going on
do {
// whatever needs to be done periodically
// (while this happens, the counter could get incremented)
// when done, decrement and test the counter:
n=Interlocked.Decrement(ref activationCounter); // decrements in a safe way
} while (n!=0); // check more is required
}:)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
Hi Luc, Sorry for the delay in my response and thank you for your responses. I couldn't implement your code, I am not sure if I am doing something wrong. I have put in another posting requesting guidance on building a service with a thread that looks at a folder for files and processes the file. Upon completion of the processing, it starts over again. Help if you can please on that.
Sameer
-
bool stop = false;
...
Thread t = new Thread(new ThreadStart(run)){IsBackground = true}.Start();
...private void run(){
while(!stop){
Thread.Sleep(60000);
MonitorFunc();
}
} -
bool stop = false;
...
Thread t = new Thread(new ThreadStart(run)){IsBackground = true}.Start();
...private void run(){
while(!stop){
Thread.Sleep(60000);
MonitorFunc();
}
}That is not really what the OP wanted, since it will pause for an entire minute in between two executions, whereas the OP wanted it to run every minute. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Hi Luc, Sorry for the delay in my response and thank you for your responses. I couldn't implement your code, I am not sure if I am doing something wrong. I have put in another posting requesting guidance on building a service with a thread that looks at a folder for files and processes the file. Upon completion of the processing, it starts over again. Help if you can please on that.
Sameer
Saamir wrote:
I couldn't implement your code
that is extremely vague and uninformative, no one can help you based on this. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.