stopping threads

T

Tony Johansson

Hi!

I'm still reading the book from Microsoft Press (exam 70-536) and there is
something that I don't fully understand.
The book says.
<start text>Controlling threads in your applications often require that you
to be able to stop threads.
The primary mechanism for stopping threads is to use the Thread.Abort
method. When the Thread.Abort method is called, the threading system
prepares to throw a ThreadAbortException in the Thread. Whether the
exception is caught or not, the thread is stopped after it is thrown. The
following code snippet provides an example. More text below the code

Thread newThread = new Thread(new ThreadStart(AbortThisThread));
newThread .Start();
newThread .Abort();

static void AbortThisThread)=
{
SomeClass.IsValid = true;
SomeClass.IsComplete = true;
SomeClass.WriteToConsole();
}

Because the AbortThisThread method never catches the ThreadAbortException,
this thread stop at the line currently executing when the main thread calls
Abort to kill the thread.<end text>

Now to my question it's the last row in the text that bother me because it
says
Because the AbortThisThread method never catches the ThreadAbortException,
this thread stop at the line currently executing when the main thread calls
Abort to kill the thread.
I mean it would have been exactly the same if the sample had caught the
ThreadAbortException meaning this thread will stop at the line currently
executing so as a summary it doesn't matter whether or not if you catch the
ThreadAbortException the thread will still stop at the line currently
executing when a Abort is done on this thread.

I'm I right or have I missed somthing ?

//Tony
 
P

Peter Duniho

Tony said:
[...]
I mean it would have been exactly the same if the sample had caught the
ThreadAbortException meaning this thread will stop at the line currently
executing so as a summary it doesn't matter whether or not if you catch the
ThreadAbortException the thread will still stop at the line currently
executing when a Abort is done on this thread.

I'm I right or have I missed somthing ?

All you've done is reiterate what the text already says (that the thread
will stop whether the exception is caught or not).

That said, I've said it before and I'll say it again: that book you are
using is a piece of crap.

Thread.Abort isn't even a reasonable way to signal to a thread to stop,
never mind the _primary_ way.

Go find a decent reference.

Pete
 
A

Arne Vajhøj

I'm still reading the book from Microsoft Press (exam 70-536) and there is
something that I don't fully understand.
The book says.
<start text>Controlling threads in your applications often require that you
to be able to stop threads.
The primary mechanism for stopping threads is to use the Thread.Abort
method. When the Thread.Abort method is called, the threading system
prepares to throw a ThreadAbortException in the Thread. Whether the
exception is caught or not, the thread is stopped after it is thrown. The
following code snippet provides an example. More text below the code

Thread newThread = new Thread(new ThreadStart(AbortThisThread));
newThread .Start();
newThread .Abort();

static void AbortThisThread)=
{
SomeClass.IsValid = true;
SomeClass.IsComplete = true;
SomeClass.WriteToConsole();
}

Because the AbortThisThread method never catches the ThreadAbortException,
this thread stop at the line currently executing when the main thread calls
Abort to kill the thread.<end text>

Now to my question it's the last row in the text that bother me because it
says
Because the AbortThisThread method never catches the ThreadAbortException,
this thread stop at the line currently executing when the main thread calls
Abort to kill the thread.
I mean it would have been exactly the same if the sample had caught the
ThreadAbortException meaning this thread will stop at the line currently
executing so as a summary it doesn't matter whether or not if you catch the
ThreadAbortException the thread will still stop at the line currently
executing when a Abort is done on this thread.

I'm I right or have I missed somthing ?

1) You should not use Thread.Abort to stop a thread with. Set a flag
and let the thread stop itself. With Thread.Abort you don't know
in what state you will end up.

2) The code is pretty bad, because there are no guarantee that
the thread may not have completed when Abort is called.

3) Obviously the code stops where it stops. But ThreadAbortException
can be catched and various things can happen. See
http://msdn.microsoft.com/en-us/library/ty8d3wta.aspx for
some details.

Arne
 

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

Top