I have a code that works fine, when you abort the background thread it aborts it properly (causes the ThreadAbortException)
however taking the same piece of code and modifying it slightly (not even touching the thread properties) doesnt seem to abort the thread at all!
//Method 1 or something void SomeMethod() try { ... ... do stuff ... } catch (ThreadAbortException) { this.theRunningThread = null; } private void cmdStart_Click(object sender, System.EventArgs e) { this.theRunningThread = new ThreadStart(SomeMethod); this.theRunningThread.IsBackground = true; this.theRunningThread.Start(); } private void cmdStop_Click(object sender, System.EventArgs e) { if (this.theRunningThread != null) { this.theRunningThread.Abort(System.Threading.ThreadState.Background); } } |
does this code look ok to you

Threading problems
jdcompman
Crazy68aseric
//Method 1 or something
void SomeMethod()
try
{
...
... do stuff
...
}
catch (ThreadAbortException)
{
this.theRunningThread = null;
}
private void cmdStart_Click(object sender, System.EventArgs e)
{
this.theRunningThread = new Thread(new ThreadStart(SomeMethod));
this.theRunningThread.IsBackground = true;
this.theRunningThread.Start();
}
private void cmdStop_Click(object sender, System.EventArgs e)
{
if (this.theRunningThread != null)
{
this.theRunningThread.Abort(System.Threading.ThreadState.Background);
}
}
.Hibri
www.hibri.net
Mus_C
well sure if your using locks then yeh of course....
please provide an example on how you would "release all locks" and clean up the thread when the thread has been finished executing its job to do.... :)
would be useful to myself and others
Ged
Abort is bad because mutexes held by aborted thread may not be released properly, causing deadlock to occur.
The recommended solution is for thread to periodically check a flag to see if it should stop processing and it cleans itself properly by releasing all locks when it has been asked to terminate.
Adec
it still causes the same problems
Early
A better way is to create a flag which the function tests and uses to abort gracefully.
Shaun Bedingfield
blogsb.blogspot.com
shaunbed@houston.rr.com
nlitement
I have a similar problem. My thread executes a function running a Directory.GetFiles() that may be take some times, especially if the directory is big and located on remote computer.
I would like to be able to abort the directory scan if its takes to long.
If Thread.Abort() is not a 'clean' solution for interrupting the thread, what could I use to get some quite immediate stop for such an expensive function
Thanks for your answer.