471,605 Members | 1,327 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,605 software developers and data experts.

Best/fastest way to abort worker thread

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

Feb 8 '06 #1
5 3365
Btw. it's in .NET 1.1 (2.0 is not an option pt.)

Soren
"Soren S. Jorgensen" <no****@nodomain.com> skrev i en meddelelse
news:O%****************@TK2MSFTNGP15.phx.gbl...
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

Feb 8 '06 #2
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]
- mv*@spam.guard.caspershouse.com

"Soren S. Jorgensen" <no****@nodomain.com> wrote in message
news:O%****************@TK2MSFTNGP15.phx.gbl...
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

Feb 8 '06 #3
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> skrev i
en meddelelse news:uc**************@TK2MSFTNGP10.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Soren S. Jorgensen" <no****@nodomain.com> wrote in message
news:O%****************@TK2MSFTNGP15.phx.gbl...
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


Feb 8 '06 #4
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> skrev i
en meddelelse news:uc**************@TK2MSFTNGP10.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Soren S. Jorgensen" <no****@nodomain.com> wrote in message
news:O%****************@TK2MSFTNGP15.phx.gbl...
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


Feb 8 '06 #5
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]

"Soren S. Jorgensen" <no****@nodomain.com> wrote in message
news:O%****************@TK2MSFTNGP15.phx.gbl...
| 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
|
|
|
Feb 8 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jeff | last post: by
7 posts views Thread by Morris | last post: by
20 posts views Thread by Doug Thews | last post: by
6 posts views Thread by LP | last post: by
1 post views Thread by Ennixo | last post: by
18 posts views Thread by Urs Vogel | last post: by
5 posts views Thread by andrew | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

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.