The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

haacked (1) [Avatar] Offline
#1
Hello. I enjoyed the book, but have a question about using Thread.Abort(). I get the impression that you recommend using Thread.Abort() when monitoring a thread to stop it over using a status variable and having the thread check the status var.

Is that the case? According to this MSDN article http://msdn.microsoft.com/library/en-us/cpguide/html/cpconmanagedthreadingbestpractices.asp
they recommend against using Thread.Abort() on another thread. Are there cases that you feel are exceptions to this guideline?
DamonCarr (7) [Avatar] Offline
#2
Re: Using Thread.Abort()?
Microsoft is correct (no disrespect Dan - I presented on Multithreading tonight and gave away your book. I will email you my slides). Aborting a thread should be avoided for a number of reasons.
From

Juval Lowey, one of my favorite authors, See:

http://www.baynetug.org/uploads/Productive%20.NET%20with%20Juval%20Lowy%20.pdf


Killing a Thread
------------------
Do not call Abort()
Thread may need to do clean-up
Abort() does not allow graceful exit
The thread method should check a flag
Protect flag with mutex
Kill() method should set flag and wait for thread to terminate
Abort() has another flaw: Thread can do indefinite processing in catch{} Application

public class WorkerThread : IDisposable
{
protected Thread m_ThreadObj;
protected bool m_EndLoop;
protected Mutex m_EndLoopMutex;
protected bool EndLoop
{
set
{
m_EndLoopMutex.WaitOne();
m_EndLoop = value;
m_EndLoopMutex.ReleaseMutex();
}
get
{
bool result = false;
m_EndLoopMutex.WaitOne();
result = m_EndLoop;
m_EndLoopMutex.ReleaseMutex();
return result;
}
}
public WorkerThread()
{
m_EndLoop = false;
m_ThreadObj = null;
m_EndLoopMutex = new Mutex();
}

Kind Regards,
Damon Wilder Carr, Chief Technologist and CEO

.NET SIG Leader - New York Software Industry Association (NYSIA www.nysia.org)
Founder and President: 'Association for the Advancement of Software Engineering in .NET' (AASET)
Microsoft Certified Partner
Visual Studio Industry Partner Affiliate
Professional Member - Association for Computing Machinery (www.acm.org)


agilefactor
80 Broad Street
5th Floor
New York, NY 10004

Voice 212.837.7788
Fax 212.859.7359

http://www.agilefactor.com/
http://agiledamon.blogspot.com/