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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Cannot abort thread

Cannot abort thread

Scheduled Pinned Locked Moved C#
helpquestion
8 Posts 4 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.
  • M Offline
    M Offline
    Mertli Ozgur Nevres
    wrote on last edited by
    #1

    Hi all, I wrote a multi-thread application using sockets. But when i attempt to stop a thread, the thread is falling into "AbortRequested" state but never being aborted. Can anyone help me? My code is below: if ( this.m_ReceiveThread.IsAlive ) MessageBox.Show( "alive" ); MessageBox.Show( "State: " + this.m_ReceiveThread.ThreadState.ToString() ); if ( this.m_ReceiveThread != null ) { for ( int i = 0; i <= 10; i++ ) { try { this.m_ReceiveThread.Abort(); this.m_ReceiveThread.Join( 100 ); /* !!!!!! it always says current state is AbortRequested !!!!!! */ MessageBox.Show( "m_ReceiveThread trying to abort. - " + i.ToString() + "\nCurrentState: " + this.m_ReceiveThead.ThreadState.ToString() ); }//End of try. catch { }//End of catch. if ( this.m_ReceiveThread.ThreadState == ThreadState.Aborted || this.m_ReceiveThread.ThreadState == ThreadState.Stopped ) { MessageBox.Show( "m_ReceiveThread aborted." ); break; }//End of if ( this.m_ReceiveThread.ThreadState == ThreadState.Aborted ). }//End of for( int i = 0; i <= 10; i++ ). }// End of if ( this.m_ReceiveThread != null ).

    M 1 Reply Last reply
    0
    • M Mertli Ozgur Nevres

      Hi all, I wrote a multi-thread application using sockets. But when i attempt to stop a thread, the thread is falling into "AbortRequested" state but never being aborted. Can anyone help me? My code is below: if ( this.m_ReceiveThread.IsAlive ) MessageBox.Show( "alive" ); MessageBox.Show( "State: " + this.m_ReceiveThread.ThreadState.ToString() ); if ( this.m_ReceiveThread != null ) { for ( int i = 0; i <= 10; i++ ) { try { this.m_ReceiveThread.Abort(); this.m_ReceiveThread.Join( 100 ); /* !!!!!! it always says current state is AbortRequested !!!!!! */ MessageBox.Show( "m_ReceiveThread trying to abort. - " + i.ToString() + "\nCurrentState: " + this.m_ReceiveThead.ThreadState.ToString() ); }//End of try. catch { }//End of catch. if ( this.m_ReceiveThread.ThreadState == ThreadState.Aborted || this.m_ReceiveThread.ThreadState == ThreadState.Stopped ) { MessageBox.Show( "m_ReceiveThread aborted." ); break; }//End of if ( this.m_ReceiveThread.ThreadState == ThreadState.Aborted ). }//End of for( int i = 0; i <= 10; i++ ). }// End of if ( this.m_ReceiveThread != null ).

      M Offline
      M Offline
      Marc Clifton
      wrote on last edited by
      #2

      Thread.Abort: Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread. Calling this method usually terminates the thread. I like the word "usually". Look at the example in MSDN. Are you catching all exceptions in the thread and not properly handling ThreadAbortException? Marc Latest AAL Article My blog Join my forum!

      K 1 Reply Last reply
      0
      • M Marc Clifton

        Thread.Abort: Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread. Calling this method usually terminates the thread. I like the word "usually". Look at the example in MSDN. Are you catching all exceptions in the thread and not properly handling ThreadAbortException? Marc Latest AAL Article My blog Join my forum!

        K Offline
        K Offline
        Kentamanos
        wrote on last edited by
        #3

        To add to what Marc said, take a look at this.


        I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
        -David St. Hubbins

        M 1 Reply Last reply
        0
        • K Kentamanos

          To add to what Marc said, take a look at this.


          I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
          -David St. Hubbins

          M Offline
          M Offline
          Marc Clifton
          wrote on last edited by
          #4

          Kentamanos wrote: To add to what Marc said, take a look at this. A thread will not terminate correctly if some process are still running (consuming) a protected ressource. Ah yes! Excellent! I'm in the middle of working with some serial I/O code in C# that uses protected resources, and it's the wierdest thing to exit the application and still see the application running in the process list even after doing an abort on the receiver thread. Needless to say, I wasn't cleaning things up. :-D Marc Latest AAL Article My blog Join my forum!

          D 1 Reply Last reply
          0
          • M Marc Clifton

            Kentamanos wrote: To add to what Marc said, take a look at this. A thread will not terminate correctly if some process are still running (consuming) a protected ressource. Ah yes! Excellent! I'm in the middle of working with some serial I/O code in C# that uses protected resources, and it's the wierdest thing to exit the application and still see the application running in the process list even after doing an abort on the receiver thread. Needless to say, I wasn't cleaning things up. :-D Marc Latest AAL Article My blog Join my forum!

            D Offline
            D Offline
            David Stone
            wrote on last edited by
            #5

            Marc Clifton wrote: some serial I/O code in C# Marc...why aren't you just using the System.IO.Ports.SerialPort class? :rolleyes: ;)


            When I can talk about 64 bit processors and attract girls with my computer not my car, I'll come out of the closet. Until that time...I'm like "What's the ENTER key?" -Hockey on being a geek

            M 1 Reply Last reply
            0
            • D David Stone

              Marc Clifton wrote: some serial I/O code in C# Marc...why aren't you just using the System.IO.Ports.SerialPort class? :rolleyes: ;)


              When I can talk about 64 bit processors and attract girls with my computer not my car, I'll come out of the closet. Until that time...I'm like "What's the ENTER key?" -Hockey on being a geek

              M Offline
              M Offline
              Marc Clifton
              wrote on last edited by
              #6

              Nice to know I'm on your "to read" list. :-D David Stone wrote: Marc...why aren't you just using the System.IO.Ports.SerialPort class? Because I'm not using Longhorn! :doh: And, is there some reason they didn't inlude the parallel port in the Ports namespace? Oh yes, I forgot--Microsoft wishes the thing had never been invented! Marc Latest AAL Article My blog Join my forum!

              D 1 Reply Last reply
              0
              • M Marc Clifton

                Nice to know I'm on your "to read" list. :-D David Stone wrote: Marc...why aren't you just using the System.IO.Ports.SerialPort class? Because I'm not using Longhorn! :doh: And, is there some reason they didn't inlude the parallel port in the Ports namespace? Oh yes, I forgot--Microsoft wishes the thing had never been invented! Marc Latest AAL Article My blog Join my forum!

                D Offline
                D Offline
                David Stone
                wrote on last edited by
                #7

                Marc Clifton wrote: Nice to know I'm on your "to read" list. Heh...I was just browsing through the forums. ;) Marc Clifton wrote: Because I'm not using Longhorn! Well...you don't have to use Longhorn. You could just use Whidbey. ;P


                When I can talk about 64 bit processors and attract girls with my computer not my car, I'll come out of the closet. Until that time...I'm like "What's the ENTER key?" -Hockey on being a geek

                M 1 Reply Last reply
                0
                • D David Stone

                  Marc Clifton wrote: Nice to know I'm on your "to read" list. Heh...I was just browsing through the forums. ;) Marc Clifton wrote: Because I'm not using Longhorn! Well...you don't have to use Longhorn. You could just use Whidbey. ;P


                  When I can talk about 64 bit processors and attract girls with my computer not my car, I'll come out of the closet. Until that time...I'm like "What's the ENTER key?" -Hockey on being a geek

                  M Offline
                  M Offline
                  Marc Clifton
                  wrote on last edited by
                  #8

                  David Stone wrote: You could just use Whidbey. Uh huh. For production software? Marc Latest AAL Article My blog Join my forum!

                  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