Hi Raj,
Thank you for posting.
When a call is made to the Abort method to destroy a thread, the common
language runtime throws a ThreadAbortException. ThreadAbortException is a
special exception that can be caught. When dealting with the
ThreadAbortException, you could use Thread.ResetAbort to cancel the abort
and go on executing the rest code. And you may call the Thread.Join method
if you want to wait until the aborted thread has ended. Join is a blocking
call that does not return until the thread actually stops executing.
The following is a sample on how to dealt with the termination of thread.
public class ThreadWork {
public static void DoWork() {
try {
for(int i=0; i<100; i++) {
Console.WriteLine("Thread - working.");
Thread.Sleep(100);
}
}
catch(ThreadAbortException e) {
Console.WriteLine("Thread - caught ThreadAbortException -
resetting.");
Console.WriteLine("Exception message: {0}", e.Message);
Thread.ResetAbort();
}
Console.WriteLine("Thread - still alive and working.");
Thread.Sleep(1000);
Console.WriteLine("Thread - finished working.");
}
}
class ThreadAbortTest {
public static void Main() {
ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
Thread myThread = new Thread(myThreadDelegate);
myThread.Start();
Thread.Sleep(100);
Console.WriteLine("Main - aborting my thread.");
myThread.Abort();
myThread.Join();
Console.WriteLine("Main ending.");
}
}
Hope this is helpful to you.
If you have any other concerns or need anything else, please don't hesitate
to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================