473,216 Members | 1,868 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,216 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 3497
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()...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.