471,338 Members | 950 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Using Timer: the application thread count keeps increasing


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
2 3348
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
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.

Similar topics

2 posts views Thread by Amadej | last post: by
3 posts views Thread by redneon | last post: by
4 posts views Thread by gsimmons | last post: by

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.