In an Windows application I have created a Timer to monitor some
WorkerThread's state. Once the worker thread stops the timer should
also dispose after displaying the message that Worker Thread is done.
// Instance variables
private Thread workerThread = null;
private System.Threading.Timer workerThreadMonitorTimer = null;
/// Code inside a button click event
ThreadStart workerThreadStart = new ThreadStart(DoWork);
this.workerThread = new Thread(migrationProcessStart);
this.workerThread.Start();
TimerCallback timerCallBack = new TimerCallback(CheckWorkerThread);
this.workerThreadMonitorTimer = new
System.Threading.Timer(timerCallBack);
this.workerThreadMonitorTimer.Change(0, 5000);
In the CheckWorkerThread method I wrote:
if (this.workerThread.ThreadState ==
System.Threading.ThreadState.Stopped)
{
// Display some message
this.workerThreadMonitorTimer.Dispose();
}
But the timer does not get disposed. It still executes the
CheckWorkerThread method next time as well. So message is getting
displayed multiple times. Its not even ending.