Solved

Thread.Abort Memory Management

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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