Solved

Thread.Abort Memory Management

Posted on 2006-10-20
1
225 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

816 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

12 Experts available now in Live!

Get 1:1 Help Now