473,394 Members | 1,751 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.

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 3510
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jeff | last post by:
I am running a worker thread that manipulates some hardware setting every so often. My problem is that the hardware manipulation cannot be interrupted once it has started How can I ensure that...
4
by: Stephan Steiner | last post by:
Hi I'm having some weird threading issues.. almost at random, if I dare change a line of my code, the shutdown sequence gets messed up. I'm using a thread to receive data from the network, that...
7
by: Morris | last post by:
I want to abort a running thread, so I call MyThread.abort() function. My problem is this thread runs "almost" like a while(true) loop and I don't want the Abort() function interrupts the thread at...
20
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a...
6
by: LP | last post by:
Hi, I am starting a new thread from the main UI thread. If users clicks cancel button, the thread is aborted: workerThread.Abort(); ThreadAbort Exception is handeled in the worker thread: ...
1
by: Ennixo | last post by:
hi, i coded an application in which i can move a slider to define a radius and it computes a gaussian blur in a thread. because the Scroll event of the slider is often raised, i use one thread...
18
by: Urs Vogel | last post by:
Hi I wrote an application server (a remoting sinlgeton), where processes must be stopped in very rare cases, done thru a Thread.Abort(). Occasionally, and only after a Thread.Abort(), this...
5
by: andrew | last post by:
Hi, I have the following issue with the Thread.Abort(): The main thread creates a worker thread which waits on a process termination. void ThreadProc() { Process proc =...
7
by: Marc Bartsch | last post by:
Hi, I have a background worker in my C# app that makes a synchronous HttpWebRequest.GetResponse() call. The idea is to POST a file to a server on the internet. When I call HttpWebRequest.Abort()...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.