Best/fastest way to abort worker thread

  • Thread starter Soren S. Jorgensen
  • Start date
S

Soren S. Jorgensen

Hi,

In my app I've got a worker thread (background) doing some calculations
based upon user input. A new worker thread might be invoked before the
previous worker thread has ended, and I wan't only one worker thread running
at any time (if a new worker thread start has been requested, any running
worker thread results will be invalid). I'm using the below method to invoke
a new worker thread, but when stress testing this I'm sometimes getting a
System.Threading.ThreadStateException (Thread is suspended; attempting to
abort) and everything locks up.

Does anyone now of a more fail safe method to successfully kill a ongoing
thread ??

private void StartWorker()
{
lock(this)
{
if(_calcThread != null)
{
while(_calcThread.IsAlive)
{
if(_calcThread.ThreadState !=
System.Threading.ThreadState.AbortRequested &&
_calcThread.ThreadState !=
System.Threading.ThreadState.SuspendRequested &&
_calcThread.ThreadState !=
System.Threading.ThreadState.Suspended)
{
_calcThread.Abort();
}
else
{
_calcThread.Join(50);
}
}

_calcThread = null;
}

_calcThread = new Thread(new ThreadStart(this.RunCalculation));
_calcThread.IsBackground = true;
_calcThread.Priority = ThreadPriority.Highest;
_calcThread.Start();
}

Thanks...

Soren
 
N

Nicholas Paldino [.NET/C# MVP]

Soren,

You shouldn't call Abort on threads for this purpose.

How you should terminate another thread is dependent on how you perform
processing in that thread.

If you are looping in the thread, performing your processing, then you
should check a shared variable (which has access to it synchronized, of
course) to see if the processing should stop. If you need to stop, then you
just exit the function when that variable indicates that the thread should
shut down.

If the processing is linear, then you will need to use a shared variable
as well, but you will have to decide at which points you check to see if
your processing should stop.

Now, you also want to know when the thread terminates. For this, you
could call Join on the thread (not in the thread itself, but on the instance
representing the worker thread) and wait for it to finish (of course, you
would only do this once you set your flag indicating that the thread should
stop).

However, I think that using Join is bad practice. Rather, you should
create an event, and signal the event in the worker thread when it is done.

Then, you can wait for the event to be signaled in the main thread, and
continue.

Hope this helps.
 
S

Soren S. Jorgensen

Hi Nicolas,

Yes, I agree that previous posted example might not to be the best way to
end a worker thread, but the issue here is that the proccessing is actually
done in another module (and I'd really hate to begin changing this). So
there's really not a lot of points where it would be possible check if an
event is set signaling the worker thread to stop processing.
When prossecing is successfully done (or if another processing request has
not been submitted), the result is returned (from the worker) to main thread
through a delegate, in any other case it will/should be discarded. So the
only place where it would be suitable checking a signal would be just before
returning the result at the end of the thread. This will also do, but will
take up unnessecary resources on the client machine.

Soren

Nicholas Paldino said:
Soren,

You shouldn't call Abort on threads for this purpose.

How you should terminate another thread is dependent on how you perform
processing in that thread.

If you are looping in the thread, performing your processing, then you
should check a shared variable (which has access to it synchronized, of
course) to see if the processing should stop. If you need to stop, then
you just exit the function when that variable indicates that the thread
should shut down.

If the processing is linear, then you will need to use a shared
variable as well, but you will have to decide at which points you check to
see if your processing should stop.

Now, you also want to know when the thread terminates. For this, you
could call Join on the thread (not in the thread itself, but on the
instance representing the worker thread) and wait for it to finish (of
course, you would only do this once you set your flag indicating that the
thread should stop).

However, I think that using Join is bad practice. Rather, you should
create an event, and signal the event in the worker thread when it is
done.

Then, you can wait for the event to be signaled in the main thread, and
continue.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Soren S. Jorgensen said:
Hi,

In my app I've got a worker thread (background) doing some calculations
based upon user input. A new worker thread might be invoked before the
previous worker thread has ended, and I wan't only one worker thread
running at any time (if a new worker thread start has been requested, any
running worker thread results will be invalid). I'm using the below
method to invoke a new worker thread, but when stress testing this I'm
sometimes getting a System.Threading.ThreadStateException (Thread is
suspended; attempting to abort) and everything locks up.

Does anyone now of a more fail safe method to successfully kill a ongoing
thread ??

private void StartWorker()
{
lock(this)
{
if(_calcThread != null)
{
while(_calcThread.IsAlive)
{
if(_calcThread.ThreadState !=
System.Threading.ThreadState.AbortRequested &&
_calcThread.ThreadState !=
System.Threading.ThreadState.SuspendRequested &&
_calcThread.ThreadState !=
System.Threading.ThreadState.Suspended)
{
_calcThread.Abort();
}
else
{
_calcThread.Join(50);
}
}

_calcThread = null;
}

_calcThread = new Thread(new ThreadStart(this.RunCalculation));
_calcThread.IsBackground = true;
_calcThread.Priority = ThreadPriority.Highest;
_calcThread.Start();
}

Thanks...

Soren
 
S

Soren S. Jorgensen

Hi Nicolas,

Yes, I agree that previous posted example might not to be the best way to
end a worker thread, but the issue here is that the proccessing is actually
done in another module (and I'd really hate to begin changing this). So
there's really not a lot of points where it would be possible check if an
event is set signaling the worker thread to stop processing.
When prossecing is successfully done (or if another processing request has
not been submitted), the result is returned (from the worker) to main thread
through a delegate, in any other case it will/should be discarded. So the
only place where it would be suitable checking a signal would be just before
returning the result at the end of the thread. This will also do, but will
take up unnessecary resources on the client machine.

Soren

Nicholas Paldino said:
Soren,

You shouldn't call Abort on threads for this purpose.

How you should terminate another thread is dependent on how you perform
processing in that thread.

If you are looping in the thread, performing your processing, then you
should check a shared variable (which has access to it synchronized, of
course) to see if the processing should stop. If you need to stop, then
you just exit the function when that variable indicates that the thread
should shut down.

If the processing is linear, then you will need to use a shared
variable as well, but you will have to decide at which points you check to
see if your processing should stop.

Now, you also want to know when the thread terminates. For this, you
could call Join on the thread (not in the thread itself, but on the
instance representing the worker thread) and wait for it to finish (of
course, you would only do this once you set your flag indicating that the
thread should stop).

However, I think that using Join is bad practice. Rather, you should
create an event, and signal the event in the worker thread when it is
done.

Then, you can wait for the event to be signaled in the main thread, and
continue.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Soren S. Jorgensen said:
Hi,

In my app I've got a worker thread (background) doing some calculations
based upon user input. A new worker thread might be invoked before the
previous worker thread has ended, and I wan't only one worker thread
running at any time (if a new worker thread start has been requested, any
running worker thread results will be invalid). I'm using the below
method to invoke a new worker thread, but when stress testing this I'm
sometimes getting a System.Threading.ThreadStateException (Thread is
suspended; attempting to abort) and everything locks up.

Does anyone now of a more fail safe method to successfully kill a ongoing
thread ??

private void StartWorker()
{
lock(this)
{
if(_calcThread != null)
{
while(_calcThread.IsAlive)
{
if(_calcThread.ThreadState !=
System.Threading.ThreadState.AbortRequested &&
_calcThread.ThreadState !=
System.Threading.ThreadState.SuspendRequested &&
_calcThread.ThreadState !=
System.Threading.ThreadState.Suspended)
{
_calcThread.Abort();
}
else
{
_calcThread.Join(50);
}
}

_calcThread = null;
}

_calcThread = new Thread(new ThreadStart(this.RunCalculation));
_calcThread.IsBackground = true;
_calcThread.Priority = ThreadPriority.Highest;
_calcThread.Start();
}

Thanks...

Soren
 
W

William Stacey [MVP]

Use one worker thread and blocking queue. The worker blocks on empty queue.
When you post work to the queue the thread starts and does its thing. It
can post back to your UI using an event or calling a method on the form that
BeginInvokes a method on the UI thread.
Here is example using a blocking queue:
http://channel9.msdn.com/ShowPost.aspx?PostID=161030

--
William Stacey [MVP]

| Hi,
|
| In my app I've got a worker thread (background) doing some calculations
| based upon user input. A new worker thread might be invoked before the
| previous worker thread has ended, and I wan't only one worker thread
running
| at any time (if a new worker thread start has been requested, any running
| worker thread results will be invalid). I'm using the below method to
invoke
| a new worker thread, but when stress testing this I'm sometimes getting a
| System.Threading.ThreadStateException (Thread is suspended; attempting to
| abort) and everything locks up.
|
| Does anyone now of a more fail safe method to successfully kill a ongoing
| thread ??
|
| private void StartWorker()
| {
| lock(this)
| {
| if(_calcThread != null)
| {
| while(_calcThread.IsAlive)
| {
| if(_calcThread.ThreadState !=
| System.Threading.ThreadState.AbortRequested &&
| _calcThread.ThreadState !=
| System.Threading.ThreadState.SuspendRequested &&
| _calcThread.ThreadState !=
| System.Threading.ThreadState.Suspended)
| {
| _calcThread.Abort();
| }
| else
| {
| _calcThread.Join(50);
| }
| }
|
| _calcThread = null;
| }
|
| _calcThread = new Thread(new ThreadStart(this.RunCalculation));
| _calcThread.IsBackground = true;
| _calcThread.Priority = ThreadPriority.Highest;
| _calcThread.Start();
| }
|
| Thanks...
|
| Soren
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top