473,229 Members | 1,764 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,229 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 3473
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 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...
3
by: redneon | last post by:
I have a program which is constantly reading from a stream and what I'm wanting to do is, if the stream hasn't sent anything after a certain amount of time then do something. I've tried doing this...
9
by: George McCullen | last post by:
I have an Outlook 2003 using Exchange Server 2003 Public Contacts Folder containing 20,000 Contacts. I am writing a VB .Net 2003 program that loops through all the contacts in a "for each oCt in...
6
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically,...
7
by: RobKinney1 | last post by:
Hello, Wow...I have one for you all and hopefully I am not understanding this timer object correctly. I have a timer setup that pulses a connection through a socket every 60 seconds. But it...
4
by: gsimmons | last post by:
I've been researching multi-threaded WinForms apps and thread synchronization stuff for a couple days since I'm working on refactoring a multi-threaded GUI app at work and want to be sure it's...
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: Amit Dedhia | last post by:
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...
3
by: Cartoper | last post by:
My application appears to have a recourse leak. When the user starts a background process, the handle count in Process Explorer (PE) goes up by about 10, sometime 1 or 2 more, sometimes 1 or 2...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.