By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,432 Members | 795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,432 IT Pros & Developers. It's quick & easy.

Using Timer: the application thread count keeps increasing

P: n/a

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

Jun 4 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Jun 4, 3:58 am, Amit Dedhia <amitded...@yahoo.comwrote:
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 ?

Jun 4 '07 #2

P: n/a
On Jun 4, 12:58 pm, Amit Dedhia <amitded...@yahoo.comwrote:
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

Jun 4 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.