Thread pool

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

As we know thread pool threads are background by default. Suppose that we
change them to foreground as soon as they are started, they do some task and
exit. The problem is that the application never ends.
This is a sample:

private void MyThread(object state)
{
Thread.CurrentThread.IsBackground = false ;
}


And we have a button in a windows form application:

private void btnThread_Click(object sender, System.EventArgs e)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(MyThread)) ;
}

As we see, thread pool thread does nothing and exits right away. When we
close the window theoritically the application should terminate but task
manager says opposite. Why?
 
Reza said:
Hi

As we know thread pool threads are background by default. Suppose
that we change them to foreground as soon as they are started,
they do some task and exit. The problem is that the application
never ends.

This is a sample:

private void MyThread(object state)
{
Thread.CurrentThread.IsBackground = false ;
}


And we have a button in a windows form application:

private void btnThread_Click(object sender, System.EventArgs e)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(MyThread)) ;
}

As we see, thread pool thread does nothing and exits right away.
When we close the window theoritically the application should
terminate but task manager says opposite. Why?

It may appear that the thread does nothing and exits right away, but in
reality it is waiting for another work item to be queued. That's how
thread pools work. That is, they create several threads and run an
infite loop on each one that waits for delegates to be queued and then
executes them. Because they do not terminate like you expected the
application will continue to run if they are changed to foreground
threads.

If there is something specific you are trying to accomplish we may be
able to offer some suggestions.

Brian
 
Back
Top