473,583 Members | 2,878 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why does Thread class not support IDisposable?

Why does Thread class not support IDisposable? It's creating quite
some problem. Namely, it can exhaust the resource and you have not
control over it.
Dec 7 '07 #1
34 2772
On Dec 7, 2:19 pm, Creativ <GongXinr...@gm ail.comwrote:
Why does Thread class not support IDisposable?
Why should it? What would Thread.Dispose actually do?
It's creating quite some problem. Namely, it can exhaust the resource and you
have not control over it.
I've done a fair amount of threading and never got into a situation
where I want to dispose of a thread. Now terminating a thread in a
graceful manner is a different matter - but that's up to the
collaboration between the threads. If you need to "hard" reset a
thread, Abort is your friend - but you should only really do that if
you're tearing down the process (or at least the AppDomain) as it
leaves things in an indeterminate state.

Jon
Dec 7 '07 #2
The scenario I can think of is you create a thread to run some task.
After some runs, quite some threads will be created. After doing that
for a long time, you will have problem in creating Thread and run it
since the CloseHandle will be called in the Finalizer.

Dec 7 '07 #3
On Dec 7, 2:37 pm, Creativ <GongXinr...@gm ail.comwrote:
The scenario I can think of is you create a thread to run some task.
After some runs, quite some threads will be created. After doing that
for a long time, you will have problem in creating Thread and run it
since the CloseHandle will be called in the Finalizer.
If you've got that many actual threads, you've got bigger problems
anyway. Have you actually run into this as a problem? Have you seen
anyone else running into this as a problem?

Jon
Dec 7 '07 #4
Yes, I did. Some one created a lot of thread to run some calculation.
Finally he get an error from the numeric library, "Reached Thread
Limit".
I adviced him to use ThreadPool.
But idealy, Thread should support IDisposable since it's a limited
resource.
Dec 7 '07 #5
On Dec 7, 3:17 pm, Creativ <GongXinr...@gm ail.comwrote:
Yes, I did. Some one created a lot of thread to run some calculation.
Finally he get an error from the numeric library, "Reached Thread
Limit".
But did you isolate the problem to finalization rather than it
physically trying to run too many threads?
I adviced him to use ThreadPool.
But idealy, Thread should support IDisposable since it's a limited
resource.
But it's not something that *can* be manually released. There's the
handle, but that's it - and I suspect that's not what you were seeing
in the above situation.

As I said before, if you're creating that many threads you will run
into issues regardless.

Jon
Dec 7 '07 #6
As I said before, if you're creating that many threads you will run
into issues regardless.
The problem is as following. Since the Finalize() can be really
delayed, the program will hold a lot of thread handle when those
theads are ready. Implementing a Dispose which call CloseHandle() will
solve this problem.
Dec 7 '07 #7
Changing to ThreadPool, you won't get that kind problem.
Dec 7 '07 #8
On Dec 7, 3:32 pm, Creativ <GongXinr...@gm ail.comwrote:
As I said before, if you're creating that many threads you will run
into issues regardless.

The problem is as following. Since the Finalize() can be really
delayed, the program will hold a lot of thread handle when those
theads are ready. Implementing a Dispose which call CloseHandle() will
solve this problem.
Only if you know exactly when to call it - which you typically don't.

There are typically many, many, *many* times more handles available
than you sensibly want to create threads - in other words, if you're
creating enough threads to run into this as a problem, then you're
doing something wrong anyway.

It's like exceptions: there's a slight performance hit, but if you're
throwing enough to see a *significant* issue, then you're almost
certainly misusing exceptions in the first place.

Jon
Dec 7 '07 #9
"Creativ" <Go*********@gm ail.comwrote:
Yes, I did. Some one created a lot of thread to run some calculation.
Finally he get an error from the numeric library, "Reached Thread
Limit".
I've gotta agree with Jon on this one. It's not a limitation in Windows
Threading, or the implementation of .Net thread constructs. The problem here
is the algorithm being used is deeply flawed.
I adviced him to use ThreadPool.
But idealy, Thread should support IDisposable since it's a limited
resource.
Threads are special. There's no way an external source can Dispose a thread.
It just isn't something that makes logical sense given the design of threads
in Windows.

An external source can set a flag, "Please Exit when you can", and the
thread can choose to honor that flag. Such a construct is common.

--
Chris Mullins
Dec 7 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2790
by: Daniel Billingsley | last post by:
The example code for Memory shows it being used in a using() block. The documentation for using() says it can only be used on things that implement Disposable. Yet there is no Dispose() method for Memory. So, while using(Memory...) compiles fine, calling .Dispose() in a finally block wouldn't be possible. Those are supposed to be...
5
2088
by: Barry Anderberg | last post by:
I'm using a tool by Sci-Tech called the .NET Memory Profiler. We have a massive .NET/C# application here and it has been exhibiting memory leak behavior for some time. Attempting to remedy the problems I am employing the aforementioned software in trying to track down the problems. After an hour or so of program execution, a snapshot of...
5
3784
by: admin | last post by:
ok This is my main. Pretty much it goes through each category and starts up 4 worker threads that then ask for groups to gether from. My problem is that when the thread gets done it keeps the mysql connections open so I end up with quite a few at the end. Is there a different way that I should do this? class Program { static string...
71
10705
by: active | last post by:
In the main program I check to see if a certain form has been disposed. Does it make sense in that form's FormClosed event to do: Me.Dispose to make sure it is disposed the next time I check. Or is that automatic? Thanks
1
2890
by: =?Utf-8?B?c3VydHVyeg==?= | last post by:
Hi, I'm using VB2005 + Office XP Enterprise. If you create a Word.Application object, you risk a memory leak if your application crashes because Word.Application is an unmanaged COM Interop object. I want to encapsulate the Word.Application in a Class that implements IDisposable to avoid this potential memory leak. Here is the code I...
15
7407
by: Barry Flynn | last post by:
VB 2005. I have the following code in a Sub. Dim oFred As SillyClass oFred = New SillyClass oFred.Gloop() oFred = Nothing Exit Sub
0
8159
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8314
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7922
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8185
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6571
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5689
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3836
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1416
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.