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. CWinThread Termination

CWinThread Termination

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
7 Posts 5 Posters 5 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.
  • L Offline
    L Offline
    Lakshminaryana A
    wrote on last edited by
    #1

    How to Terminate a thread created from CWinThread, without checking flag in thread function or ::TerminateThread (WinAPI)? Example: CWinThread* Obj; Obj = AfxBeginThread(ThreadProc, this); UINT ThreadProc(LPVOID *pVoid) { while(1) { //do something } return 0; } Now I want to terminate the ThreadProc without using flag or TeminateThread? Thanks in Adavance :)

    L J V 3 Replies Last reply
    0
    • L Lakshminaryana A

      How to Terminate a thread created from CWinThread, without checking flag in thread function or ::TerminateThread (WinAPI)? Example: CWinThread* Obj; Obj = AfxBeginThread(ThreadProc, this); UINT ThreadProc(LPVOID *pVoid) { while(1) { //do something } return 0; } Now I want to terminate the ThreadProc without using flag or TeminateThread? Thanks in Adavance :)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Why not just use the features that are available?

      1 Reply Last reply
      0
      • L Lakshminaryana A

        How to Terminate a thread created from CWinThread, without checking flag in thread function or ::TerminateThread (WinAPI)? Example: CWinThread* Obj; Obj = AfxBeginThread(ThreadProc, this); UINT ThreadProc(LPVOID *pVoid) { while(1) { //do something } return 0; } Now I want to terminate the ThreadProc without using flag or TeminateThread? Thanks in Adavance :)

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #3

        Just exit the thread function using a return statement (which is missing but required in your example code). After adding the missing return statement, you may then also break out of the endless while loop.

        L 1 Reply Last reply
        0
        • J Jochen Arndt

          Just exit the thread function using a return statement (which is missing but required in your example code). After adding the missing return statement, you may then also break out of the endless while loop.

          L Offline
          L Offline
          Lakshminaryana A
          wrote on last edited by
          #4

          Thank you for your response I want to terminate the ThreadProc from outside of the thread

          J 1 Reply Last reply
          0
          • L Lakshminaryana A

            Thank you for your response I want to terminate the ThreadProc from outside of the thread

            J Offline
            J Offline
            Jochen Arndt
            wrote on last edited by
            #5

            That was not really clear. Then you have to signal the thread that it should terminate. If you don't want to use a global flag, use some kind of event. To check for events use WaitForSingleObject or WaitForMultipleObject (when multiple events must be handled). Examples can be found in any MFC thread tutorial. An old but very good article is: Using Worker Threads[^] (see the Thread Shutdown Without Polling section). Also available at WorkerThreads[^].

            1 Reply Last reply
            0
            • L Lakshminaryana A

              How to Terminate a thread created from CWinThread, without checking flag in thread function or ::TerminateThread (WinAPI)? Example: CWinThread* Obj; Obj = AfxBeginThread(ThreadProc, this); UINT ThreadProc(LPVOID *pVoid) { while(1) { //do something } return 0; } Now I want to terminate the ThreadProc without using flag or TeminateThread? Thanks in Adavance :)

              V Offline
              V Offline
              Victor Nijegorodov
              wrote on last edited by
              #6

              Member 13147508 wrote:

              How to Terminate a thread created from CWinThread, without checking flag in thread function or ::TerminateThread (WinAPI)? Example: CWinThread* Obj; Obj = AfxBeginThread(ThreadProc, this); UINT ThreadProc(LPVOID *pVoid) { while(1) { //do something } return 0; }

              The cleanest way to exit the thread is to return from the thread procedure. And it is exactlz what your ThreadProc does! So, nothing more!

              D 1 Reply Last reply
              0
              • V Victor Nijegorodov

                Member 13147508 wrote:

                How to Terminate a thread created from CWinThread, without checking flag in thread function or ::TerminateThread (WinAPI)? Example: CWinThread* Obj; Obj = AfxBeginThread(ThreadProc, this); UINT ThreadProc(LPVOID *pVoid) { while(1) { //do something } return 0; }

                The cleanest way to exit the thread is to return from the thread procedure. And it is exactlz what your ThreadProc does! So, nothing more!

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

                See the OP's response here.

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                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