Solved

Thread.Abort Memory Management

Posted on 2006-10-20
1
229 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…

738 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