Solved

Thread.Abort Memory Management

Posted on 2006-10-20
1
224 Views
Last Modified: 2013-12-03
I have an application that spawns a thread to handle some intense processing.  My problem comes in when my process makes some calls to a external dll that ends up with a pointer to some memory.  When everything runs like it should, the pointer gets disposed of in the thread before it finishes.  My problem is that sometimes a person will close this specific form before the thread is finished running.  At the form level I am doing a Thread.Abort to close the thread, but I need to determine how dispose of that memory being retained by the pointer.  It is being created and disposed of in methods called from the thread.  Can someone give me any suggestions?????

Thanks
Kendal
0
Comment
Question by:gvector1
1 Comment
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 17778995
If main thread uses Thread.Abort to stop worker thread, worker thread code must look like this:

try
{
     // worker thread code
}
catch(ThreadAbortException ex)
{
    // thread is stopped
    //
    // clean-up code
    // ...
    Thread.ResetAbort();
}
finally  // optionally
{
    // clean-up code
}

Writing thread code, you need to carefully manage variables state and execute all required cleanup code in catch or finally block. This is not so easy because thread can be aborted in any place and resources allocated by thread code can be in undefined state.
I don't like this way to stop thread and think that it is dirty, especially if thread code allocates unmanaged resources. I think that it is better to take full control in the code: thread must have Stop event associated with it. When main thread needs to stop worker thread, it sets this event and waits for thread exit using Join. Thread code must periodically test this event. If it is signaled, thread executes cleanup code and exits.
http://www.codeproject.com/csharp/workerthread.asp
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now