473,394 Members | 2,002 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,394 software developers and data experts.

Starting a new Thread vs. ThreadPool

Hello,

I have always used a certain design pattern for multithreaded Windows app;
Start new worker thread from UI thread, use events to notify UI threads when
something happens, update UI controls using delegates through .BeginInvoke. I
came across some code samples recently where ThreadPool class is used to
start new threads.
My questions;
1. what's the difference?
2. Any performance benefits?
3. Can ThreadPool be used for the above-mentioned design pattern?

If you could provide some insights on ThreadPool and specific instances when
ThreadPool should (or must) be used, I would greatly appreciate it.
Thank you
Nov 17 '05 #1
10 8734


Lenn wrote:
Hello,

I have always used a certain design pattern for multithreaded Windows app;
Start new worker thread from UI thread, use events to notify UI threads when
something happens, update UI controls using delegates through .BeginInvoke. I
came across some code samples recently where ThreadPool class is used to
start new threads. My questions;
1. what's the difference?
There are only 25 threads in the threadpool, code queued to execute must
await the termination of a running thread in the pool.

This means that you could get into a starvation situation where none of
the threads in the pool terminate because the threads that updates the
state so they can continue cannot run :)

If you had started a thread instead you might have 1000s of them
running, and you get an error when you try to *start* the thread, not a
deadlock.
2. Any performance benefits?
Try a profiler, post it here if you find any difference worth
mentioning. Remember that (hopefully) less that 1% of your code is
spawning threads, so you only get very little of any bechmarked improvement.
3. Can ThreadPool be used for the above-mentioned design pattern?
You are *already* using the threadpool in the exact way it's meant to be
used: to run short pieces of code that will soon terminate. .BeginInvoke
queues execution on the threadpool.
If you could provide some insights on ThreadPool and specific instances when
ThreadPool should (or must) be used, I would greatly appreciate it.


There is no code for which it should or must be used, let the compiler
use it for .BeginInvoke.

Do not use it for things that you expect to block, waiting for other
threads.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #2


Lenn wrote:
Hello,

I have always used a certain design pattern for multithreaded Windows app;
Start new worker thread from UI thread, use events to notify UI threads when
something happens, update UI controls using delegates through .BeginInvoke. I
came across some code samples recently where ThreadPool class is used to
start new threads. My questions;
1. what's the difference?
There are only 25 threads in the threadpool, code queued to execute must
await the termination of a running thread in the pool.

This means that you could get into a starvation situation where none of
the threads in the pool terminate because the threads that updates the
state so they can continue cannot run :)

If you had started a thread instead you might have 1000s of them
running, and you get an error when you try to *start* the thread, not a
deadlock.
2. Any performance benefits?
Try a profiler, post it here if you find any difference worth
mentioning. Remember that (hopefully) less that 1% of your code is
spawning threads, so you only get very little of any bechmarked improvement.
3. Can ThreadPool be used for the above-mentioned design pattern?
You are *already* using the threadpool in the exact way it's meant to be
used: to run short pieces of code that will soon terminate. .BeginInvoke
queues execution on the threadpool.
If you could provide some insights on ThreadPool and specific instances when
ThreadPool should (or must) be used, I would greatly appreciate it.


There is no code for which it should or must be used, let the compiler
use it for .BeginInvoke.

Do not use it for things that you expect to block, waiting for other
threads.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #3

Lenn wrote:
Hello,

I have always used a certain design pattern for multithreaded Windows app;
Start new worker thread from UI thread, use events to notify UI threads when
something happens, update UI controls using delegates through .BeginInvoke. I
came across some code samples recently where ThreadPool class is used to
start new threads.
My questions; 1. what's the difference?
The ThreadPool is managed by the framework. It is a fixed size pool of
threads that the framework uses to run work items asynchronously. It's
very easy to use. You simply make a call to
ThreadPool.QueueUserWorkItem and pass in a delegate of the method you
want to run.
2. Any performance benefits?
Yes, especially for work items that execute quickly. Since the threads
have already been created there is little overhead in getting a work
item to execute.
3. Can ThreadPool be used for the above-mentioned design pattern?
It depends. If the task you're wanting to run asynchronously takes a
long time to complete then no. You're better off managing your own
worker thread. If the task completes very quickly then it might be a
candidate. If you could explain what your existing worker thread does
then we could offer better advice.
If you could provide some insights on ThreadPool and specific instances when
ThreadPool should (or must) be used, I would greatly appreciate it.
Thank you


Like I said, generally tasks that complete quickly are candidates.

Nov 17 '05 #4

Lenn wrote:
Hello,

I have always used a certain design pattern for multithreaded Windows app;
Start new worker thread from UI thread, use events to notify UI threads when
something happens, update UI controls using delegates through .BeginInvoke. I
came across some code samples recently where ThreadPool class is used to
start new threads.
My questions; 1. what's the difference?
The ThreadPool is managed by the framework. It is a fixed size pool of
threads that the framework uses to run work items asynchronously. It's
very easy to use. You simply make a call to
ThreadPool.QueueUserWorkItem and pass in a delegate of the method you
want to run.
2. Any performance benefits?
Yes, especially for work items that execute quickly. Since the threads
have already been created there is little overhead in getting a work
item to execute.
3. Can ThreadPool be used for the above-mentioned design pattern?
It depends. If the task you're wanting to run asynchronously takes a
long time to complete then no. You're better off managing your own
worker thread. If the task completes very quickly then it might be a
candidate. If you could explain what your existing worker thread does
then we could offer better advice.
If you could provide some insights on ThreadPool and specific instances when
ThreadPool should (or must) be used, I would greatly appreciate it.
Thank you


Like I said, generally tasks that complete quickly are candidates.

Nov 17 '05 #5
Helge Jensen wrote:
Lenn wrote:
Hello,

I have always used a certain design pattern for multithreaded Windows app;
Start new worker thread from UI thread, use events to notify UI threads when
something happens, update UI controls using delegates through .BeginInvoke. I
came across some code samples recently where ThreadPool class is used to
start new threads.

My questions;
1. what's the difference?


There are only 25 threads in the threadpool, code queued to execute must
await the termination of a running thread in the pool.

This means that you could get into a starvation situation where none of
the threads in the pool terminate because the threads that updates the
state so they can continue cannot run :)

If you had started a thread instead you might have 1000s of them
running, and you get an error when you try to *start* the thread, not a
deadlock.
2. Any performance benefits?


Try a profiler, post it here if you find any difference worth
mentioning. Remember that (hopefully) less that 1% of your code is
spawning threads, so you only get very little of any bechmarked improvement.
3. Can ThreadPool be used for the above-mentioned design pattern?


You are *already* using the threadpool in the exact way it's meant to be
used: to run short pieces of code that will soon terminate. .BeginInvoke
queues execution on the threadpool.


The OP is not using the ThreadPool currently. The BeginInvoke method
the OP is speaking of is actually ISynchronizeInvoke.BeginInvoke. When
used on a Form or Control this method marshals the execution a of
delegate onto the thread hosting that form or control (the UI thread).
In this case execution is queued and executed by the message loop.
If you could provide some insights on ThreadPool and specific instances when
ThreadPool should (or must) be used, I would greatly appreciate it.


There is no code for which it should or must be used, let the compiler
use it for .BeginInvoke.

Do not use it for things that you expect to block, waiting for other
threads.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-


Nov 17 '05 #6
Helge Jensen wrote:
Lenn wrote:
Hello,

I have always used a certain design pattern for multithreaded Windows app;
Start new worker thread from UI thread, use events to notify UI threads when
something happens, update UI controls using delegates through .BeginInvoke. I
came across some code samples recently where ThreadPool class is used to
start new threads.

My questions;
1. what's the difference?


There are only 25 threads in the threadpool, code queued to execute must
await the termination of a running thread in the pool.

This means that you could get into a starvation situation where none of
the threads in the pool terminate because the threads that updates the
state so they can continue cannot run :)

If you had started a thread instead you might have 1000s of them
running, and you get an error when you try to *start* the thread, not a
deadlock.
2. Any performance benefits?


Try a profiler, post it here if you find any difference worth
mentioning. Remember that (hopefully) less that 1% of your code is
spawning threads, so you only get very little of any bechmarked improvement.
3. Can ThreadPool be used for the above-mentioned design pattern?


You are *already* using the threadpool in the exact way it's meant to be
used: to run short pieces of code that will soon terminate. .BeginInvoke
queues execution on the threadpool.


The OP is not using the ThreadPool currently. The BeginInvoke method
the OP is speaking of is actually ISynchronizeInvoke.BeginInvoke. When
used on a Form or Control this method marshals the execution a of
delegate onto the thread hosting that form or control (the UI thread).
In this case execution is queued and executed by the message loop.
If you could provide some insights on ThreadPool and specific instances when
ThreadPool should (or must) be used, I would greatly appreciate it.


There is no code for which it should or must be used, let the compiler
use it for .BeginInvoke.

Do not use it for things that you expect to block, waiting for other
threads.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-


Nov 17 '05 #7


Brian Gideon wrote:
Helge Jensen wrote: The OP is not using the ThreadPool currently. The BeginInvoke method
the OP is speaking of is actually ISynchronizeInvoke.BeginInvoke.


I see.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #8


Brian Gideon wrote:
Helge Jensen wrote: The OP is not using the ThreadPool currently. The BeginInvoke method
the OP is speaking of is actually ISynchronizeInvoke.BeginInvoke.


I see.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #9
Thank you all,

From what I am hearing ThreadPool will not make my life easier in any form.
So far, I developed a few .NET applications, that automate certain production
tasks. Mostly has to do with pulling data from SQL Server and/or legacy file
server, processing it, and generating reports. So, I usually start a worker
thread that in turn makes calls to different objects that do all those
smaller tasks, worker thread raises events when certain tasks complete or
fail. Worker thread itself might spun a few new threads for processing IO
Streams.
Depending on how much data it has to deal with, it can run from 4sec to
couple of hours. Of course nice UI with progress bars keeps user updated with
what's going on.
So far, it worked great for the user, I thought maybe I could put each
smaller tasks in ThreadPool, but now I don't think it would make that much of
a difference.
"Brian Gideon" wrote:

Lenn wrote:
Hello,

I have always used a certain design pattern for multithreaded Windows app;
Start new worker thread from UI thread, use events to notify UI threads when
something happens, update UI controls using delegates through .BeginInvoke. I
came across some code samples recently where ThreadPool class is used to
start new threads.
My questions;

1. what's the difference?


The ThreadPool is managed by the framework. It is a fixed size pool of
threads that the framework uses to run work items asynchronously. It's
very easy to use. You simply make a call to
ThreadPool.QueueUserWorkItem and pass in a delegate of the method you
want to run.
2. Any performance benefits?


Yes, especially for work items that execute quickly. Since the threads
have already been created there is little overhead in getting a work
item to execute.
3. Can ThreadPool be used for the above-mentioned design pattern?


It depends. If the task you're wanting to run asynchronously takes a
long time to complete then no. You're better off managing your own
worker thread. If the task completes very quickly then it might be a
candidate. If you could explain what your existing worker thread does
then we could offer better advice.
If you could provide some insights on ThreadPool and specific instances when
ThreadPool should (or must) be used, I would greatly appreciate it.
Thank you


Like I said, generally tasks that complete quickly are candidates.

Nov 17 '05 #10
Thank you all,

From what I am hearing ThreadPool will not make my life easier in any form.
So far, I developed a few .NET applications, that automate certain production
tasks. Mostly has to do with pulling data from SQL Server and/or legacy file
server, processing it, and generating reports. So, I usually start a worker
thread that in turn makes calls to different objects that do all those
smaller tasks, worker thread raises events when certain tasks complete or
fail. Worker thread itself might spun a few new threads for processing IO
Streams.
Depending on how much data it has to deal with, it can run from 4sec to
couple of hours. Of course nice UI with progress bars keeps user updated with
what's going on.
So far, it worked great for the user, I thought maybe I could put each
smaller tasks in ThreadPool, but now I don't think it would make that much of
a difference.
"Brian Gideon" wrote:

Lenn wrote:
Hello,

I have always used a certain design pattern for multithreaded Windows app;
Start new worker thread from UI thread, use events to notify UI threads when
something happens, update UI controls using delegates through .BeginInvoke. I
came across some code samples recently where ThreadPool class is used to
start new threads.
My questions;

1. what's the difference?


The ThreadPool is managed by the framework. It is a fixed size pool of
threads that the framework uses to run work items asynchronously. It's
very easy to use. You simply make a call to
ThreadPool.QueueUserWorkItem and pass in a delegate of the method you
want to run.
2. Any performance benefits?


Yes, especially for work items that execute quickly. Since the threads
have already been created there is little overhead in getting a work
item to execute.
3. Can ThreadPool be used for the above-mentioned design pattern?


It depends. If the task you're wanting to run asynchronously takes a
long time to complete then no. You're better off managing your own
worker thread. If the task completes very quickly then it might be a
candidate. If you could explain what your existing worker thread does
then we could offer better advice.
If you could provide some insights on ThreadPool and specific instances when
ThreadPool should (or must) be used, I would greatly appreciate it.
Thank you


Like I said, generally tasks that complete quickly are candidates.

Nov 17 '05 #11

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...
12
by: serge calderara | last post by:
Dear all, I have an application which is suppose to start another executable process. As soon as that process is running, I need to retrive its handle. The problem of the particular process I am...
2
by: objectref | last post by:
hi to all folks, i want to create the following senario: i have a T1 (Thread 1) that acts like http server that receives incoming requests. From that T1, i want to spawn from T1 to Tn thread...
0
by: Lenn | last post by:
Hello, I have always used a certain design pattern for multithreaded Windows app; Start new worker thread from UI thread, use events to notify UI threads when something happens, update UI...
19
by: trint | last post by:
Ok, I start my thread job: Thread t = new Thread(new ThreadStart(invoicePrintingLongRunningCodeThread)); t.IsBackground = true; t.Start(); There are lots of calls to controls and many...
10
by: Andrew Bullock | last post by:
Hi, code: myClass x = new myClass(); x.dosomethingwith(x,y); How do i make dosomethingwith(x,y) run on a separate thread, and then inform the main thread when it has finished?
7
by: Sin Jeong-hun | last post by:
Hi. I'm writing a Client/Multi-threaded Server program on Windows Vista. It worked fine on Windows Vista, but when the server ran on Windows XP, I/O operation has been aborted because of either...
34
by: Creativ | last post by:
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.
7
by: Curious | last post by:
On Jun 10, 3:32 am, <s...@dailycoding.comwrote: Thanks! I'll use thread pool.
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...
0
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...
0
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...

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.