Correct way to abort a Suspended thread?
-
I'm using a background thread in my Windows Forms app to retrieve all user objects in our Active Directory. (15,000+ users) I am retrieving users in lots of 1,000, at which time I call the Suspend() method on my thread to pause the GUI and allow them to select a user or users. If the user presses the Cancel button while the thread is in the running state, I call the Abort() method and catch the corresponding ThreadAbortException, and the thread terminates gracefully. The problem I'm having is that if I call the Abort() method while the thread is in a suspended state, the thread never terminates. I am catching the ThreadStateException that is thrown, but is there something else I must do to the thread to properly terminate it at this point? Code follows: private void buttonMemberSelectCancel_Click(object sender, System.EventArgs e) { try { if (!bgListThread.IsAlive) // is alive? this.Close(); else { try { bgListThread.Abort(); } catch(System.Threading.ThreadAbortException) { // abort thread gracefully } catch(System.Threading.ThreadStateException) { // if suspended, abort gracefully } } } catch (NullReferenceException) { // if thread doesn't exist, just close dialog } finally { this.Close(); } }
-
I'm using a background thread in my Windows Forms app to retrieve all user objects in our Active Directory. (15,000+ users) I am retrieving users in lots of 1,000, at which time I call the Suspend() method on my thread to pause the GUI and allow them to select a user or users. If the user presses the Cancel button while the thread is in the running state, I call the Abort() method and catch the corresponding ThreadAbortException, and the thread terminates gracefully. The problem I'm having is that if I call the Abort() method while the thread is in a suspended state, the thread never terminates. I am catching the ThreadStateException that is thrown, but is there something else I must do to the thread to properly terminate it at this point? Code follows: private void buttonMemberSelectCancel_Click(object sender, System.EventArgs e) { try { if (!bgListThread.IsAlive) // is alive? this.Close(); else { try { bgListThread.Abort(); } catch(System.Threading.ThreadAbortException) { // abort thread gracefully } catch(System.Threading.ThreadStateException) { // if suspended, abort gracefully } } } catch (NullReferenceException) { // if thread doesn't exist, just close dialog } finally { this.Close(); } }
In my unscientific tests, I noticed that the Thread.Abort() method cannot cancel an interop call, only managed code.
It's not the fall that kills you: it's the sudden stop - Down by Law, Jim Jamursch (1986)
-
In my unscientific tests, I noticed that the Thread.Abort() method cannot cancel an interop call, only managed code.
It's not the fall that kills you: it's the sudden stop - Down by Law, Jim Jamursch (1986)
Daniel Turini wrote: I noticed that the Thread.Abort() method cannot cancel an interop call, only managed code. Huh? I'm not making any Interop calls in this function at all.
-
I'm using a background thread in my Windows Forms app to retrieve all user objects in our Active Directory. (15,000+ users) I am retrieving users in lots of 1,000, at which time I call the Suspend() method on my thread to pause the GUI and allow them to select a user or users. If the user presses the Cancel button while the thread is in the running state, I call the Abort() method and catch the corresponding ThreadAbortException, and the thread terminates gracefully. The problem I'm having is that if I call the Abort() method while the thread is in a suspended state, the thread never terminates. I am catching the ThreadStateException that is thrown, but is there something else I must do to the thread to properly terminate it at this point? Code follows: private void buttonMemberSelectCancel_Click(object sender, System.EventArgs e) { try { if (!bgListThread.IsAlive) // is alive? this.Close(); else { try { bgListThread.Abort(); } catch(System.Threading.ThreadAbortException) { // abort thread gracefully } catch(System.Threading.ThreadStateException) { // if suspended, abort gracefully } } } catch (NullReferenceException) { // if thread doesn't exist, just close dialog } finally { this.Close(); } }
It's a gruesome suggestion, but just as a test, you might try resuming the thread and then aborting it.
-
It's a gruesome suggestion, but just as a test, you might try resuming the thread and then aborting it.
Maybe, but according to the MSDN docs, the CLR already does that for you when you call Abort.
-
I'm using a background thread in my Windows Forms app to retrieve all user objects in our Active Directory. (15,000+ users) I am retrieving users in lots of 1,000, at which time I call the Suspend() method on my thread to pause the GUI and allow them to select a user or users. If the user presses the Cancel button while the thread is in the running state, I call the Abort() method and catch the corresponding ThreadAbortException, and the thread terminates gracefully. The problem I'm having is that if I call the Abort() method while the thread is in a suspended state, the thread never terminates. I am catching the ThreadStateException that is thrown, but is there something else I must do to the thread to properly terminate it at this point? Code follows: private void buttonMemberSelectCancel_Click(object sender, System.EventArgs e) { try { if (!bgListThread.IsAlive) // is alive? this.Close(); else { try { bgListThread.Abort(); } catch(System.Threading.ThreadAbortException) { // abort thread gracefully } catch(System.Threading.ThreadStateException) { // if suspended, abort gracefully } } } catch (NullReferenceException) { // if thread doesn't exist, just close dialog } finally { this.Close(); } }
try Give Your .NET-based Application a Fast and Responsive UI with Multiple Threads by Ian Griffits
-
Maybe, but according to the MSDN docs, the CLR already does that for you when you call Abort.
Marc Merritt wrote: according to the MSDN docs, the CLR already does that for you when you call Abort. I don't know if that has always been in the docs, but it certainly isn't what happens under .NET v1.0; in v1.1 it has been corrected and works correctly. James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation