Timer dispose does not end thread

A

Amadej

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 disposed, do not seem to
end their thread after the timers stop working).

For instance, when making a simple timer with the following code:

public System.Threading.Timer timerTest;
public System.Threading.TimerCallback tCallBack;

....

tCallBack = new System.Threading.TimerCallback(Callback);
timerTest = new System.Threading.Timer(tCallBack,null, 3000, 3000);

....

public void Callback(object state)
{ iCount++;
label1.Text = iCount.ToString();
if (iCount == 10)
timerTest.Dispose();
}

The program's thread count would jump from 3, to 6 (sometimes even
7?), but when the iCount would reach 10, and the dispose for the timer
would be called, the thread count would not drop. So based on that I
am presuming, the threads for the timer do not actually stop, but are
rather suspended?

On the other hand, when I create a custom thread and stop it, the
thread count in Windows Task Manager does decrease appropriately. So
why are the threads for the timer staying "alive"? How can I clean up
the resources? (I have tried invoking garbage collector, but the
threads do not get stopped, based on the Task Manager).

Thank you for any advice/answers in advance,
Amadej.
 
S

Sami Vaaraniemi

Amadej said:
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 disposed, do not seem to
end their thread after the timers stop working).
[snip]
On the other hand, when I create a custom thread and stop it, the
thread count in Windows Task Manager does decrease appropriately. So
why are the threads for the timer staying "alive"? How can I clean up
the resources? (I have tried invoking garbage collector, but the
threads do not get stopped, based on the Task Manager).

Thank you for any advice/answers in advance,
Amadej.

System.Threading.Timer uses a thread from the thread pool (see
System.Threading.ThreadPool class). These threads are managed by the system
and you don't need to worry about terminating them. Everything should be
fine resource-wise as long as you call Dispose on the timer objects.

Regards,
Sami
 
A

Amadej

System.Threading.Timer uses a thread from the thread pool (see
System.Threading.ThreadPool class). These threads are managed by the system
and you don't need to worry about terminating them. Everything should be
fine resource-wise as long as you call Dispose on the timer objects.

Regards,
Sami

Oh, okay I seem to have missed that part. Thank you for a quick
response Sami.

Regards,
Amadej.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top