Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Correct way to abort a Suspended thread?

Correct way to abort a Suspended thread?

Scheduled Pinned Locked Moved C#
winformswindows-adminhelpquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Christian Merritt
    wrote on last edited by
    #1

    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(); } }

    D M A 3 Replies Last reply
    0
    • C Christian Merritt

      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(); } }

      D Offline
      D Offline
      Daniel Turini
      wrote on last edited by
      #2

      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)

      C 1 Reply Last reply
      0
      • D Daniel Turini

        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)

        C Offline
        C Offline
        Christian Merritt
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • C Christian Merritt

          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(); } }

          M Offline
          M Offline
          Martin Cook
          wrote on last edited by
          #4

          It's a gruesome suggestion, but just as a test, you might try resuming the thread and then aborting it.

          C 1 Reply Last reply
          0
          • M Martin Cook

            It's a gruesome suggestion, but just as a test, you might try resuming the thread and then aborting it.

            C Offline
            C Offline
            Christian Merritt
            wrote on last edited by
            #5

            Maybe, but according to the MSDN docs, the CLR already does that for you when you call Abort.

            J 1 Reply Last reply
            0
            • C Christian Merritt

              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(); } }

              A Offline
              A Offline
              Adam Turner
              wrote on last edited by
              #6

              try Give Your .NET-based Application a Fast and Responsive UI with Multiple Threads by Ian Griffits

              1 Reply Last reply
              0
              • C Christian Merritt

                Maybe, but according to the MSDN docs, the CLR already does that for you when you call Abort.

                J Offline
                J Offline
                James T Johnson
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups