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 / C++ / MFC
  4. How to stop a thread from another thread.

How to stop a thread from another thread.

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
16 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.
  • D Offline
    D Offline
    Deepak Samuel
    wrote on last edited by
    #1

    Hi, I created a thread using the AfxBeginthread function. How do I stop this thread from another thread. MSDN resources say that its dangerous to use call TerminateThread. I tried using it too, but it failed to terminate it. Thanks, Deepak Samuel.

    2 J B 3 Replies Last reply
    0
    • D Deepak Samuel

      Hi, I created a thread using the AfxBeginthread function. How do I stop this thread from another thread. MSDN resources say that its dangerous to use call TerminateThread. I tried using it too, but it failed to terminate it. Thanks, Deepak Samuel.

      2 Offline
      2 Offline
      224917
      wrote on last edited by
      #2

      one way is to set a global boolean flag like g_fExit = true, from whichever thread you want ..so that the corresponding thread sees that and return.


      It's not a bug, it's an undocumented feature.
      suhredayan@omniquad.com

      messenger :suhredayan@hotmail.com

      J 1 Reply Last reply
      0
      • D Deepak Samuel

        Hi, I created a thread using the AfxBeginthread function. How do I stop this thread from another thread. MSDN resources say that its dangerous to use call TerminateThread. I tried using it too, but it failed to terminate it. Thanks, Deepak Samuel.

        J Offline
        J Offline
        jmkhael
        wrote on last edited by
        #3

        Check the CEvent for MFC or the events in general for WIN32 The main idea: in the loop of the thread that you want to stop, call WaitForSingleObject with WAIT_OBJECT_0 on the event handle, and in case it is signaled you just have to return :) and in the other thread just signal the event Papa while (TRUE) Papa.WillLove ( Bebe ) ;

        2 1 Reply Last reply
        0
        • 2 224917

          one way is to set a global boolean flag like g_fExit = true, from whichever thread you want ..so that the corresponding thread sees that and return.


          It's not a bug, it's an undocumented feature.
          suhredayan@omniquad.com

          messenger :suhredayan@hotmail.com

          J Offline
          J Offline
          jmkhael
          wrote on last edited by
          #4

          in case you wanna use that call InterlockedIncrement or use a Critical section object in order to prevents more than one thread from using the same variable simultaneously. Papa while (TRUE) Papa.WillLove ( Bebe ) ;

          1 Reply Last reply
          0
          • J jmkhael

            Check the CEvent for MFC or the events in general for WIN32 The main idea: in the loop of the thread that you want to stop, call WaitForSingleObject with WAIT_OBJECT_0 on the event handle, and in case it is signaled you just have to return :) and in the other thread just signal the event Papa while (TRUE) Papa.WillLove ( Bebe ) ;

            2 Offline
            2 Offline
            224917
            wrote on last edited by
            #5

            i told that was one way and perhaps the simplest. see wot Jeffery Ricther has to say. [^] "its easy to make complex and complex to make simple" :)


            It's not a bug, it's an undocumented feature.
            suhredayan@omniquad.com

            messenger :suhredayan@hotmail.com

            P 2 Replies Last reply
            0
            • 2 224917

              i told that was one way and perhaps the simplest. see wot Jeffery Ricther has to say. [^] "its easy to make complex and complex to make simple" :)


              It's not a bug, it's an undocumented feature.
              suhredayan@omniquad.com

              messenger :suhredayan@hotmail.com

              P Offline
              P Offline
              Prakash Nadar
              wrote on last edited by
              #6

              Both are easy and simple to use, but i would rather go with events as it sure that it would be thread safe.


              God is Real, unless declared Integer.

              2 1 Reply Last reply
              0
              • 2 224917

                i told that was one way and perhaps the simplest. see wot Jeffery Ricther has to say. [^] "its easy to make complex and complex to make simple" :)


                It's not a bug, it's an undocumented feature.
                suhredayan@omniquad.com

                messenger :suhredayan@hotmail.com

                P Offline
                P Offline
                Prakash Nadar
                wrote on last edited by
                #7

                suhredayan® wrote: see wot Jeffery Ricther has to say. either Jeffery is ur pal ,or you been eating pages of his book.:laugh:


                God is Real, unless declared Integer.

                2 1 Reply Last reply
                0
                • P Prakash Nadar

                  Both are easy and simple to use, but i would rather go with events as it sure that it would be thread safe.


                  God is Real, unless declared Integer.

                  2 Offline
                  2 Offline
                  224917
                  wrote on last edited by
                  #8

                  sounds nice.. please explain the need for thread saftey for this line: g_fExit = true :confused: that also with the help of an MFC object !


                  It's not a bug, it's an undocumented feature.
                  suhredayan@omniquad.com

                  messenger :suhredayan@hotmail.com

                  P 1 Reply Last reply
                  0
                  • P Prakash Nadar

                    suhredayan® wrote: see wot Jeffery Ricther has to say. either Jeffery is ur pal ,or you been eating pages of his book.:laugh:


                    God is Real, unless declared Integer.

                    2 Offline
                    2 Offline
                    224917
                    wrote on last edited by
                    #9

                    maatha>pitha>guru>god btw not a pal, but > god thats wot i have learnt.


                    It's not a bug, it's an undocumented feature.
                    suhredayan@omniquad.com

                    messenger :suhredayan@hotmail.com

                    1 Reply Last reply
                    0
                    • 2 224917

                      sounds nice.. please explain the need for thread saftey for this line: g_fExit = true :confused: that also with the help of an MFC object !


                      It's not a bug, it's an undocumented feature.
                      suhredayan@omniquad.com

                      messenger :suhredayan@hotmail.com

                      P Offline
                      P Offline
                      Prakash Nadar
                      wrote on last edited by
                      #10

                      No, i am not tring to be within the context on this problem only... In general senario where threads need to be syncronised i would use kernel objects rather than some variables.;)


                      God is Real, unless declared Integer.

                      2 1 Reply Last reply
                      0
                      • D Deepak Samuel

                        Hi, I created a thread using the AfxBeginthread function. How do I stop this thread from another thread. MSDN resources say that its dangerous to use call TerminateThread. I tried using it too, but it failed to terminate it. Thanks, Deepak Samuel.

                        B Offline
                        B Offline
                        Blake Miller
                        wrote on last edited by
                        #11

                        PostThreadMessage(ThreadId, WM_QUIT, 0, 0);

                        2 1 Reply Last reply
                        0
                        • P Prakash Nadar

                          No, i am not tring to be within the context on this problem only... In general senario where threads need to be syncronised i would use kernel objects rather than some variables.;)


                          God is Real, unless declared Integer.

                          2 Offline
                          2 Offline
                          224917
                          wrote on last edited by
                          #12

                          it 'd 'nt be fair if i dont mention the risk of deadlock. For any big and complex program it 'd be programmers nightmare to maintain all those handles. if he has lot of threads and want to stop them form others. seems you didnt faced any such problem yet.


                          It's not a bug, it's an undocumented feature.
                          suhredayan@omniquad.com

                          messenger :suhredayan@hotmail.com

                          P 1 Reply Last reply
                          0
                          • 2 224917

                            it 'd 'nt be fair if i dont mention the risk of deadlock. For any big and complex program it 'd be programmers nightmare to maintain all those handles. if he has lot of threads and want to stop them form others. seems you didnt faced any such problem yet.


                            It's not a bug, it's an undocumented feature.
                            suhredayan@omniquad.com

                            messenger :suhredayan@hotmail.com

                            P Offline
                            P Offline
                            Prakash Nadar
                            wrote on last edited by
                            #13

                            suhredayan® wrote: if he has lot of threads and want to stop them form others thats what kernel object are for, using it efficiently gives good results.


                            God is Real, unless declared Integer.

                            2 1 Reply Last reply
                            0
                            • P Prakash Nadar

                              suhredayan® wrote: if he has lot of threads and want to stop them form others thats what kernel object are for, using it efficiently gives good results.


                              God is Real, unless declared Integer.

                              2 Offline
                              2 Offline
                              224917
                              wrote on last edited by
                              #14

                              i would be greatful if you explain the pitfall in following line if executed from multiple thread. g_fExitThread=true; or atleast the need from the kernal resource, to do the above.


                              It's not a bug, it's an undocumented feature.
                              suhredayan@omniquad.com

                              messenger :suhredayan@hotmail.com

                              P 1 Reply Last reply
                              0
                              • B Blake Miller

                                PostThreadMessage(ThreadId, WM_QUIT, 0, 0);

                                2 Offline
                                2 Offline
                                224917
                                wrote on last edited by
                                #15

                                The problem with using PostThreadMessage or SendNotifyMessage is that worker threads typically don't have message loops, so these functions are not helpful always.


                                It's not a bug, it's an undocumented feature.
                                suhredayan@omniquad.com

                                messenger :suhredayan@hotmail.com

                                1 Reply Last reply
                                0
                                • 2 224917

                                  i would be greatful if you explain the pitfall in following line if executed from multiple thread. g_fExitThread=true; or atleast the need from the kernal resource, to do the above.


                                  It's not a bug, it's an undocumented feature.
                                  suhredayan@omniquad.com

                                  messenger :suhredayan@hotmail.com

                                  P Offline
                                  P Offline
                                  Prakash Nadar
                                  wrote on last edited by
                                  #16

                                  Click[^]


                                  God is Real, unless declared Integer.

                                  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