Using Timer: the application thread count keeps increasing

A

Amit Dedhia

Hi

I am developing a scientific application which has moderate level
image processing involved.

In my application, there is a main application form which invokes
another form. When this form is running, a timer function keeps
executing every 250ms. The timer function does some real time data
processing and generates a bitmap which needs to be displayed on a
picture control. This is done using this->Invoke(...) in the form.

I am using System.Threading.Timer.

Now, in the Form_CLosing event, I am disposing the timer. I have
following code for that. (C# programmers dont mind the syntax. It is C+
+/CLI)

AutoResetEvent^ disposeEvent = gcnew AutoResetEvent(false);
processingTimer->Dispose(disposeEvent);
disposeEvent->WaitOne();
delete processingTimer;

Despite this I observe that if I contineously Open and Close my form,
the thread count (as seen in the task manager window) for my
application keeps increasing. Also, sometimes the disposeEvent object
does get set and the code hangs at WaitOne(). The reason for this
could be that a particular timer instance must be waiting to finish
InvokeRequired(...) and the main thread is waiting on the disposeEvent
to get set (Deadlock situation).

So I tried using System.Timers.Timer() which has better methods like
Stop() and Close(). Now the hang problem is resolved. However the
thread count keeps increasing. One thing could be that .NET is
executing timer function on thread pool threads and just keeping then
in suspended mode. However the thread count keeps increading until
130+ (I tested only this far) and hence I am worried.

Is there a way to control the thread count? This seems like a standard
problem. Does someone has a solution?

Best regards
Amit Dedhia
 
S

SPX2

Hi

I am developing a scientific application which has moderate level
image processing involved.

In my application, there is a main application form which invokes
another form. When this form is running, a timer function keeps
executing every 250ms. The timer function does some real time data
processing and generates a bitmap which needs to be displayed on a
picture control. This is done using this->Invoke(...) in the form.

I am using System.Threading.Timer.

Now, in the Form_CLosing event, I am disposing the timer. I have
following code for that. (C# programmers dont mind the syntax. It is C+
+/CLI)

AutoResetEvent^ disposeEvent = gcnew AutoResetEvent(false);
processingTimer->Dispose(disposeEvent);
disposeEvent->WaitOne();
delete processingTimer;

Despite this I observe that if I contineously Open and Close my form,
the thread count (as seen in the task manager window) for my
application keeps increasing. Also, sometimes the disposeEvent object
does get set and the code hangs at WaitOne(). The reason for this
could be that a particular timer instance must be waiting to finish
InvokeRequired(...) and the main thread is waiting on the disposeEvent
to get set (Deadlock situation).

So I tried using System.Timers.Timer() which has better methods like
Stop() and Close(). Now the hang problem is resolved. However the
thread count keeps increasing. One thing could be that .NET is
executing timer function on thread pool threads and just keeping then
in suspended mode. However the thread count keeps increading until
130+ (I tested only this far) and hence I am worried.

Is there a way to control the thread count? This seems like a standard
problem. Does someone has a solution?

Best regards
Amit Dedhia

o isnt there a way to force the thread to close ?
 
A

adebaene

Hi

I am developing a scientific application which has moderate level
image processing involved.

In my application, there is a main application form which invokes
another form. When this form is running, a timer function keeps
executing every 250ms. The timer function does some real time data
processing and generates a bitmap which needs to be displayed on a
picture control. This is done using this->Invoke(...) in the form.

I am using System.Threading.Timer.

Now, in the Form_CLosing event, I am disposing the timer. I have
following code for that. (C# programmers dont mind the syntax. It is C+
+/CLI)

AutoResetEvent^ disposeEvent = gcnew AutoResetEvent(false);
processingTimer->Dispose(disposeEvent);
disposeEvent->WaitOne();
delete processingTimer;

Despite this I observe that if I contineously Open and Close my form,
the thread count (as seen in the task manager window) for my
application keeps increasing. Also, sometimes the disposeEvent object
does get set and the code hangs at WaitOne(). The reason for this
could be that a particular timer instance must be waiting to finish
InvokeRequired(...) and the main thread is waiting on the disposeEvent
to get set (Deadlock situation).

So I tried using System.Timers.Timer() which has better methods like
Stop() and Close(). Now the hang problem is resolved. However the
thread count keeps increasing. One thing could be that .NET is
executing timer function on thread pool threads and just keeping then
in suspended mode. However the thread count keeps increading until
130+ (I tested only this far) and hence I am worried.

Is there a way to control the thread count? This seems like a standard
problem. Does someone has a solution?

I suspect that for some reason, worker threads calling your timer's
callback get stuck somewhere (eg, a deadlock). You should check the
130+ threads you've got : Most of them should have the same call
stack, with your timer's delegate at the bottom. Check where the get
stuck (on which lock / waiting object) : this is the deadlock you need
to solve.

Arnaud
MVP - VC
 

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