473,386 Members | 2,078 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,386 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 3497
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.