473,471 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Re: New Thread or ThreadPool?

On Jun 10, 3:32*am, <s...@dailycoding.comwrote:
Using thread pool is better option. If you use any thread from thread pool,
then whenever a thread get freed, it will automatically added to the pool
and availble for reuse.
Thanks! I'll use thread pool.
Jun 27 '08 #1
7 1611
On Jun 10, 9:24*am, Curious <fir5tsi...@yahoo.comwrote:
On Jun 10, 3:32*am, <s...@dailycoding.comwrote:
Using thread pool is better option. If you use any thread from thread pool,
then whenever a thread get freed, it will automatically added to the pool
and availble for reuse.

Thanks! I'll use thread pool.
Not so fast! I disagree with the use of the ThreadPool in your case.
You said that you want to kick off a task that may not even complete
within 15 seconds. That's *way* too long to tie up a thread from the
ThreadPool. Remember, the ThreadPool may be used for things that you
are unaware of and you certainly don't want to overload it with work
that you could easy put on a dedicated thread.
Jun 27 '08 #2
On Jun 11, 10:49*am, Brian Gideon <briangid...@yahoo.comwrote:
On Jun 10, 9:24*am, Curious <fir5tsi...@yahoo.comwrote:
On Jun 10, 3:32*am, <s...@dailycoding.comwrote:
Using thread pool is better option. If you use any thread from thread pool,
then whenever a thread get freed, it will automatically added to the pool
and availble for reuse.
Thanks! I'll use thread pool.

Not so fast! *I disagree with the use of the ThreadPool in your case.
You said that you want to kick off a task that may not even complete
within 15 seconds. *That's *way* too long to tie up a thread from the
ThreadPool. *Remember, the ThreadPool may be used for things that you
are unaware of and you certainly don't want to overload it with work
that you could easy put on a dedicated thread.
Are you saying that if a task is complicated, use Thread instead of
thread pool? What are the differences between thread and thread pool
exactly?

What if I need to show a message box periodically, shall I use thread
or thread pool? If the user is in a long meeting and doesn't click
"OK" button to dismiss the message box, potentially there may be whole
bunch of message boxes clustered on the screen.

Thanks! I'll use Thread then.
Jun 27 '08 #3
On Jun 12, 9:20*am, Curious <fir5tsi...@yahoo.comwrote:
Are you saying that if a task is complicated, use Thread instead of
thread pool? What are the differences between thread and thread pool
exactly?
Thread represents a single logical thread of execution. ThreadPool is
a manager for a pool of threads that are always available to execute
work items. The advantage of the ThreadPool is that it keeps its
constituent threads initialized and running so that they can begin
executing work items immediately as they arrive. There would be no
overhead for creating a new thread and getting it running. The caveat
is that it works better if the work items can be completed quickly so
that the threads executing them can return to the pool in a timely
manner.

Think about it this way. Assume that it takes 100 CPU cycles to
initialize a new thread. Now assume that your work item takes 100 CPU
cycles to complete. The ratio of overhead to actual work is a
whopping (and unappealing) 0.5. Now assume your work item takes
10,000 cycles to complete. The ratio is now only 0.005. So in the
first example you could cut the execution time in half by using the
ThreadPool instead of spinning up a new Thread. But, in the second
example the benefit is negligible. In other words, spinning up a new
Thread in the second example doesn't effect performance all that much.
>
What if I need to show a message box periodically, shall I use thread
or thread pool? If the user is in a long meeting and doesn't click
"OK" button to dismiss the message box, potentially there may be whole
bunch of message boxes clustered on the screen.
ThreadPool versus Thread is irrelevant in this case. The execution of
your work item is a non-UI thread period. Special care needs to be
taken when attempting to interact with the UI from a non-UI thread.
In fact, you can't technically do anything to the UI (show a message
box or even read a Form variable) from a thread other than the UI
thread. If you want the work item to signal or communicate to the UI
thread then you need to marshal the execution of a method onto the UI
thread. This can be done via the Control.Invoke or
Control.BeginInvoke methods.
Thanks! I'll use Thread then.
Jun 27 '08 #4
Thanks Brian for the answer!

Now I understand that for a bigger task, it doesn't make much
difference if I were to use a thread or thread pool.

If there a limit on how many threads I can start? I assume that if
they are not started at the same time, those would be automatically
disposed by .NET if the work associated with the thread is completed.

My application is different from a UI application. It's a back-end
plug-in to an existing application with UI. It's almost impossible to
display any UI item from my back-end plug-in. However, with a new
thread, I'm able to show message boxes.
Jun 27 '08 #5
On Jun 12, 10:03*am, Curious <fir5tsi...@yahoo.comwrote:
Thanks Brian for the answer!

Now I understand that for a bigger task, it doesn't make much
difference if I were to use a thread or thread pool.
Correct. In addition, the disadvantage of using the ThreadPool for
longer running work items is that the threads in the pool will take
longer to return to the pool. That might affect the performance of
other unrelated work items that may be waiting in the queue.
>
If there a limit on how many threads I can start?
Yes. All threads consume system resources (stack space is probably
the most obvious) so at some point the system will be severely
impacted.
I assume that if
they are not started at the same time, those would be automatically
disposed by .NET if the work associated with the thread is completed.
Generally speaking yes. I don't know all of the details, but once the
method being executed by a Thread ends (or is interrupted by an
exception) then the resources being consumed by that that Thread are
returned to the system.
My application is different from a UI application. It's a back-end
plug-in to an existing application with UI. It's almost impossible to
display any UI item from my back-end plug-in. However, with a new
thread, I'm able to show message boxes.
So you're showing a message box from a worker thread then? That's all
the more reason to not to use the ThreadPool. Since message boxes are
modal they will block the execution of the caller until a user closes
the form. Honestly, I've never used a message box from a worker
thread so I don't know what the specific consequences are, but the
Show method is injecting a message loop on the calling thread to
facilitate its display so MessageBox is somewhat of a unique case.
That probably explains why it is working for you.
Jun 27 '08 #6
Hi Brian,

You sound knowledgable about multi-threading. May I ask a you a
question - I have a timer, mTimer, that runs every 15 seconds.
However, I'll only need the timer for 30 minutes. I want to destroy
mTimer after it's run for 30 minutes. May I call "mTimer.Dispose()"
from it callback? Will this cause a crash or something? FYI, my code
is below:

mTimer = new System.Threading.Timer (new
TimerCallBack(SubscribeTrade), null, 15000, 15000);
void SubscribeTrade (object state)
{
DateTime now = DateTime.Now;
TimeSpan elapsed = now.Subtract(mStartTime);
int minutes = elapsed.Minutes;

if (minutes <= 30)
{
//Code omitted here
}
else // Need to destroy the timer after 30 minutes here
{
// Is this the right way to destroy the timer object?
mTimer.Dispose();
}
}
Jun 27 '08 #7
On Jun 12, 2:46*pm, Curious <fir5tsi...@yahoo.comwrote:
Hi Brian,

You sound knowledgable about multi-threading. May I ask a you a
question - I have a timer, mTimer, that runs every 15 seconds.
However, I'll only need the timer for 30 minutes. I want to destroy
mTimer after it's run for 30 minutes. May I call "mTimer.Dispose()"
from it callback? Will this cause a crash or something? FYI, my code
is below:

mTimer = new System.Threading.Timer (new
TimerCallBack(SubscribeTrade), null, 15000, 15000);

void SubscribeTrade (object state)
{
* * DateTime now = DateTime.Now;
* * TimeSpan elapsed = now.Subtract(mStartTime);
* * int minutes = elapsed.Minutes;

* * if (minutes <= 30)
* * {
* * * * //Code omitted here
* * }
* * else // Need to destroy the timer after 30 minutes here
* * {
* * * * // Is this the right way to destroy the timer object?
* * * * mTimer.Dispose();
* * }

}
I believe that's okay, but to be sure go ahead and try it out see for
yourself.
Jun 27 '08 #8

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...
7
by: David Sworder | last post by:
Hi, I'm developing an application that will support several thousand simultaneous connections on the server-side. I'm trying to maximize throughput. The client (WinForms) and server communicate...
9
by: Drew | last post by:
I keep seeing this when I run my program in Visual Studio: The thread '<No Name>' (0x894) has exited with code 0 (0x0). What does this mean exactly? It doesn't seem to be causing a problem. ...
4
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the...
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...
10
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...
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...
4
by: Simon | last post by:
Hi All, I'm currently developping something where I need to use threads. Here's the basic scenario. I have a WebService requests which need to launch a few threads and wait for them to complete...
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.
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
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
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...
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...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.