473,326 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Timer Thread does not get disposed

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.

Sep 12 '06 #1
2 3854
Hi,
"Dipankar" <di**********@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
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;
In a win app you better use System.Windows.Forms.Timer class.
/// 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);
Do you want to call it right away? otherwise call it like Change( 5000,
5000)

In the CheckWorkerThread method I wrote:

if (this.workerThread.ThreadState ==
System.Threading.ThreadState.Stopped)
You should also first check if workerThread is null , just as a way to avoid
a nullreference exception
{
// Display some message
this.workerThreadMonitorTimer.Dispose();
}
You should call :
Change( Timeout.Infinite, Timeout.Infinite );
this.workerThreadMonitorTimer.Dispose();
this.workerThreadMonitorTimer = null;


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Sep 12 '06 #2
Hi,

What is the specific reason of using Forms.Timer rather than
Threading.Timer?
Do these two classes bahave differently?

Cheers!!
Dipankar

Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,
"Dipankar" <di**********@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
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;

In a win app you better use System.Windows.Forms.Timer class.
/// 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);

Do you want to call it right away? otherwise call it like Change( 5000,
5000)

In the CheckWorkerThread method I wrote:

if (this.workerThread.ThreadState ==
System.Threading.ThreadState.Stopped)

You should also first check if workerThread is null , just as a way to avoid
a nullreference exception
{
// Display some message
this.workerThreadMonitorTimer.Dispose();
}

You should call :
Change( Timeout.Infinite, Timeout.Infinite );
this.workerThreadMonitorTimer.Dispose();
this.workerThreadMonitorTimer = null;


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Sep 13 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Amadej | last post by:
Hello everyone I have a beginner's questions about the System.Threading.Timer class behavior. I have been observing the thread count with Windows Task Manger and noticed that timers, after being...
6
by: Jim H | last post by:
I have a class object that creates and uses a System.Threading.Timer. In the destructor for my class I cancel the timer using timer.Change(infinite, infinite) then I call timer.Dispose() to clean...
5
by: theinvisibleGhost | last post by:
I'm having a problem that occurs at random in my app. I get an exception "Cannot Access a disposed object" In MSCorLib when calling boolean Change (int32, int32) Stack trace reveals...
3
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional...
2
by: KSC | last post by:
Hello, I have used a thread timer as in the documentation on MSDN in my VB.NET application. Using System.Threading.Interlocked.Increment I increment the counter to a certain point, perform an...
1
by: Adam | last post by:
I have code that has a timer object and after running for a while it throws an error "Cannot access a disposed object named "Timer". Object name: "Timer"." I never dispose this object or any...
8
by: RobcPettit | last post by:
Hi Im using System.Timers.Timer myTimer = new System.Timers.Timer(); myTimer.Elapsed += new ElapsedEventHandler(onTimer); myTimer.Interval = 1000; myTimer.Start(); and getting error onTimer does...
5
by: Peted | last post by:
Just some threading questions 1. in a MDI app if you have multiple child forms, does each child form run in its own thread ? Eg do you get error trying to update one control on form1 from...
2
by: Johnny Jörgensen | last post by:
I've got a process I want to run in a thread separate from my main application thread, so I've used a backgroundworker component, and in frmMain.Load I invoke the code using...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.