Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Threading.Timer doesn't work

Status
Not open for further replies.

vatawna

Programmer
Feb 24, 2004
67
0
0
US
I try to schedule a job to run every 30 mins, as follows:

---
System.Threading.AutoResetEvent autoEvent = new AutoResetEvent(false);
StatusChecker statusChecker = new StatusChecker();
System.Threading.TimerCallback timerDelegate = new TimerCallback(statusChecker.CheckStatus);

System.Threading.Timer stateTimer = new System.Threading.Timer(timerDelegate, autoEvent, TimeSpan.FromSeconds(1), TimeSpan.FromMinutes(1));
---

where CheckStatus is a public procedure in the StatusChecker class, declared as follows:

public class StatusChecker {
public StatusChecker() {...}
public void CheckStatus(object stateInfo) {...}
}


The timer runs only 1 time when the program is first executed, but not recurring every 30 mins after that. Does anybody know how to fix this?

Thanks.
 
Code:
System.Threading.AutoResetEvent autoEvent = new AutoResetEvent(false);
StatusChecker statusChecker = new StatusChecker();
System.Threading.TimerCallback timerDelegate = new TimerCallback(statusChecker.CheckStatus);

System.Threading.Timer stateTimer = new System.Threading.Timer(timerDelegate, autoEvent, TimeSpan.FromSeconds(1), TimeSpan.FromMinutes(1));
The timer (i'm not sure) and/or statusChecker could have expired (GC-ed) when your code goes out of scope.
Try declaring the timer and statusChecker on the class level.
 
Possibly stupid question - is your program still running 30 minutes later?

Also - you might want to look at using the Windows Task Manager (previously known at the AT command).

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top