Thread Clean up

  • Thread starter Thread starter Raj Wall
  • Start date Start date
R

Raj Wall

Hi,

I have an application that uses a number of sub-threads.

What is the best way to do some processing in each thread when the main
application is shut down?
Is the ThreadAbortException thrown automatically for each thread? Or is
there some other event or exception automatically thrown that the thread can
"grab" as it is shut down?

Thank-you for your help!

Regards,
Raj
 
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.
====================================================
 
Linda, hi,

Thanks for the reply.

If I understand you, when my main program starts shutting down, I should
foreach loop thru my set of threads and send each an Abort message? (and
then set up to catch in the thread to shut down the thread properly).

Thanks again for your help!

Regards,
Raj
 
Hi Raj,

Thank you for your response.

Yes, you are right. Before you want to shut down your program, you should
call the Abort method of the sub-threads in your program and call the Join
method of each sub-thread to wait for them stopping executing correctly.
And in the methods the sub-threads execute, you should add try and catch
block to handle with the ThreadAbortException and use Thread.ResetAbort to
cancel the abort in the catch block.

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.
====================================================
 

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

Back
Top