Solved

Thread.Abort Memory Management

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 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

15 Experts available now in Live!

Get 1:1 Help Now