Thread Abort

  • Thread starter Thread starter Ryu
  • Start date Start date
R

Ryu

Hi, I tried to kill a thread by using the following:

Thread t = Thread.CurrentThread;
try
{
t.Abort();
t.Join()
}
catch(ThreadAbortException)
{

}

Everything when I execute from VS.NET 2003, the thread will be aborted
successfully. However when I tried to execute the program by itself, it will
always ask me to start a new instance of VS.NET. What did I do wrong?
 
I think you are starting JIT debugging due to an exception in the
thread. The abort causes an exception in your thread routine. You can
use a TRY-CATCH within the thread to handle it. A better approach is to
use something like a semaphore to signal the thread more gracefully. Use
an Abort as a last-ditch effort, perhaps if the thread does not join
within a period of time. The abort can leave things untidy.

The code you are showing should not be in the thread itself. A separate
thread will then join with the thread you are shutting down. I don't
think it will get to your join if executed within the thread you are
stopping. Within the thread, simply exit the thread, it doesn't need to
abort itself.

HTH - Lee
 
ryu said:
I don't really understand. Is it possible for you to provide an example?
news:%[email protected]...

I suppose I could. But tell me, is the code you provided intended to run
in the same thread you are trying to abort? Are you trying to get the
thread to kill itself? Or is this code intended to be run in some other
thread to kill this one? From the code you provided your intention is
not clear. The main thing is that a thread should not 1) try to abort
itself 2) try to join itself. These are things that OTHER threads do. If
you simply want the thread to stop itself this would never use
abort/join. And Abort should be used as a last-ditch "recovery" when the
normal way of getting a thread to wind down doesn't work.

- Lee
 
Ok, basically I have created a thread in a Windows Form. (Can the Win App be
considered a thread? ) And I have included the code to kill the thread under
the App's closing event. So if there anything wrong with what I did?
 
ryu said:
Ok, basically I have created a thread in a Windows Form. (Can the Win App be
considered a thread? ) And I have included the code to kill the thread under
the App's closing event. So if there anything wrong with what I did?

The windows application runs on its own thread, aka "the GUI thread".
You are aborting it rather than the thread you created.

Thread t = Thread.CurrentThread

If this code is in the closing event for the form, then the above will
get the GUI thread, not the thread you created. You need to keep an
instance of the created thread as a variable to use during closing,
rather than using CurrentThread.

The Abort and Join won't throw the exceptiong then in the closing event.

But the thread routine will.

HTH - Lee
 
Back
Top