473,322 Members | 1,232 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,322 software developers and data experts.

Threads and ThreadPool's

Threads and ThreadPool's

If I use a ThreadPool how can I tell when a thead in the threadpool has
exited? I don't want to set a global member variable I would much rather be
able to act on an event

Also (failing this ThreadPool issue) is it possible to create an array of
Worker Threads, can anyone illustrate with code please

Thanks
PT
Nov 16 '05 #1
6 2723
Max,

If you want to be notified when a thread in the ThreadPool exits, you
would have to do it the same way that you would be notified when a thread
exits. You would have to write code at the end of your method which would
trigger some sort of notification event. This could be a delegate, an
object you pass in to have a method called on it, etc, etc.

If you wanted to create an array of worker threads, you can do it like
creating an array of any other type, but in this case, I would advise
against it. You would have to handle all the management yourself, and you
are going to gain little from writing the code to determine which thread to
use, keeping the thread alive, etc, etc.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Max Adams" <ru************@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
Threads and ThreadPool's

If I use a ThreadPool how can I tell when a thead in the threadpool has
exited? I don't want to set a global member variable I would much rather be able to act on an event

Also (failing this ThreadPool issue) is it possible to create an array of
Worker Threads, can anyone illustrate with code please

Thanks
PT

Nov 16 '05 #2

I've done alot of Threaded code and I agree totally with Nicholas... Avoid
writing your own ThreadPool stuff if possible; the .NET ThreadPool is playing
games behind the scenes to be very efficient...

As far as getting notification of completion it depends on your needs.

If you can't sleep while the task runs {aka a GUI thread kicks off the task}
then kick off the task and have the last step of the task be to call
BeginInvoke on a an event that signals the GUI that the task has finished.

If you just want to know when a single task has completed then queue the
task and have the tasks last step be to set an event that wakes you up when
it's finished.

--Richard

"Max Adams" wrote:
Threads and ThreadPool's

If I use a ThreadPool how can I tell when a thead in the threadpool has
exited? I don't want to set a global member variable I would much rather be
able to act on an event

Also (failing this ThreadPool issue) is it possible to create an array of
Worker Threads, can anyone illustrate with code please

Thanks
PT

Nov 16 '05 #3
Richard <Ri*****@discussions.microsoft.com> wrote:
I've done alot of Threaded code and I agree totally with Nicholas...
Avoid writing your own ThreadPool stuff if possible; the .NET
ThreadPool is playing games behind the scenes to be very efficient...


On the other hand, it's completely out of your control and is used by
other parts of the framework, which make it somewhat dangerous in some
ways.

Personally I wish the .NET framework had a flexible ThreadPool which
could be created by an individual app and used exclusively by the app.

As it is, I don't see much wrong with using your own ThreadPool,
assuming you get a suitably robust implementation. You can then decide
what it should do in terms of security contexts etc as well. Yes, the
system thread pool has been written to be very efficient - but a lot of
people don't need quite that level of efficiency. Better to work
solidly at 95% of the optimum speed than run at 100% for a while and
then accidentally fill up the system threadpool in a way which is
unpredictable and deadlocks all the threads.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
One of my suggestion on the thread pool is to make sure you don't
create too many threads. Use the default 25 threads or lesser and
write your own function to control the pooling so that you keep only
required number of threads running all the time.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:<#U**************@TK2MSFTNGP10.phx.gbl>...
Max,

If you want to be notified when a thread in the ThreadPool exits, you
would have to do it the same way that you would be notified when a thread
exits. You would have to write code at the end of your method which would
trigger some sort of notification event. This could be a delegate, an
object you pass in to have a method called on it, etc, etc.

If you wanted to create an array of worker threads, you can do it like
creating an array of any other type, but in this case, I would advise
against it. You would have to handle all the management yourself, and you
are going to gain little from writing the code to determine which thread to
use, keeping the thread alive, etc, etc.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Max Adams" <ru************@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
Threads and ThreadPool's

If I use a ThreadPool how can I tell when a thead in the threadpool has
exited? I don't want to set a global member variable I would much rather

be
able to act on an event

Also (failing this ThreadPool issue) is it possible to create an array of
Worker Threads, can anyone illustrate with code please

Thanks
PT

Nov 16 '05 #5
On the other hand, it's completely out of your control and is used by
other parts of the framework, which make it somewhat dangerous in some
ways.

Agreed sort of: I don't like that I can't abort thread pool threads, and I
don't like that I can't join on thread pool threads, etc... but it's not out
of your control.

For most cases I've seen the thread pool does very well. The big thing I
see in people's code is that they tend to write overly complicated
synchronization logic rather than learn how to use
RegisterWaitForSingleObject() properly...
Personally I wish the .NET framework had a flexible ThreadPool which
could be created by an individual app and used exclusively by the app.

For what I'm working on right now I have pondered creating just such a more
flexible thread pool. A thread pool where I could Abort threads, set/change
base priorities, num min & max threads, automatically synchronize batch
start, and where I could WaitAll on an arbitrarily defined batch...
then accidentally fill up the system threadpool in a way which is
unpredictable and deadlocks all the threads.


Sounds like a bug in code to me - have you ever actually siezed up the pool,
I've never seen that?

--Richard
Nov 16 '05 #6
Richard <Ri*****@discussions.microsoft.com> wrote:
On the other hand, it's completely out of your control and is used by
other parts of the framework, which make it somewhat dangerous in some
ways.
Agreed sort of: I don't like that I can't abort thread pool threads,


I don't have a problem with that, as I think aborting threads should
always be a last resort which should only be used to abort the whole
app. Aborting threads can leave the system in an ill-defined state,
where you don't want to continue with the application anyway.
and I don't like that I can't join on thread pool threads, etc... but
it's not out of your control.
It is - you can't dictate the thread creation or termination policy.
You can't (easily) dictate the size. You don't get to easily control
For most cases I've seen the thread pool does very well. The big thing I
see in people's code is that they tend to write overly complicated
synchronization logic rather than learn how to use
RegisterWaitForSingleObject() properly...
Personally I wish the .NET framework had a flexible ThreadPool which
could be created by an individual app and used exclusively by the app.


For what I'm working on right now I have pondered creating just such a more
flexible thread pool. A thread pool where I could Abort threads, set/change
base priorities, num min & max threads, automatically synchronize batch
start, and where I could WaitAll on an arbitrarily defined batch...


I've started work on a ThreadPool myself, although there's definitely
more to add. It's available at

http://www.yoda.arachsys.com/csharp/.../Threading/Cus
tomThreadPool.cs

If you have a look at it and spot any bugs, please let me know :)

(There's definite refactoring needed in a few places - WorkerThreadLoop
is too big and complicated, for a start.)
then accidentally fill up the system threadpool in a way which is
unpredictable and deadlocks all the threads.


Sounds like a bug in code to me - have you ever actually siezed up the pool,
I've never seen that?


I haven't personally, although I've seen others complaining of it - and
it doesn't have to be a bug. If you've got some threads in the
threadpool waiting for other operations to complete, and those
operations are waiting for a threadpool thread to execute on, you can
end up with an effective deadlock. Don't forget that you don't always
know which operations use the ThreadPool. For instance, I'm pretty sure
that HttpWebRequest does. That's what I mean by the system ThreadPool
being out of developers' control.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

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

Similar topics

31
by: AlexeiOst | last post by:
Everywhere in documentation there are recommendations to use threads from thread pooling for relatively short tasks. As I understand, fetching a page or multiple pages (sometimes up to 50 but not...
34
by: Kovan Akrei | last post by:
Hi, I would like to know how to reuse an object of a thread (if it is possible) in Csharp? I have the following program: using System; using System.Threading; using System.Collections; ...
1
by: Novice | last post by:
Hi all, I have written some code that invokes a method a few hundred times and then it immediately finishes. I would like it to instead: 1. start the hundreds of threads (asynchronously) 2....
13
by: orekin | last post by:
Hi There I have been programming C# for a couple of months and am trying to master Threading. I understand that ThreadPool uses background threads (see code example in MSDN page titled...
3
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional...
0
by: TY | last post by:
Hi Everyone, I have a multithreading application, a typical Main thread that uses the ThreadPool.QueueUserWorkItem method to add new threads to the Thread POOL. I need a way to control the...
10
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005 and .net 2.0. I started 2 threadpool threads. How do I know when they're done with their tasks? Thanks. ThreadPool.QueueUserWorkItem(new...
3
by: UltimateBanoffee | last post by:
Hi, I'm using asp.net 2.0 and I have an understanding issue here! I don't quite understand when the available threads in the ThreadPool are ever used. The application I have running doesn't use...
3
by: tshad | last post by:
I need to find out how many threads a process has. I have used ThreadPool.GetAvailableThreads and ThreadPool.GetMaxThreads, but they both show 50 for worker threads and 100 for port threads. ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.