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 / C++ / MFC
  4. Getting Thread Exit Code

Getting Thread Exit Code

Scheduled Pinned Locked Moved C / C++ / MFC
sysadminhelpquestion
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.
  • M Offline
    M Offline
    masnu
    wrote on last edited by
    #1

    I have created a worker thread to connect to a data server. The thread needs to wait a few seconds and exit if no data arrives (i.e. server is not online). I need to trap the thread exit code so my main thread can know if the workher thread terminated with an error. I have the following code: static UINT Connect(void* p_this); UINT Connect(); UINT Client::Connect(void* p_this) { Client* pClient = (Client*)p_this; pClient->Connect(); } UINT Client::Connect() { CDataServer pServer = new CDataServer(); ........ int nError = pServer->Connect(); if ( nError != 0 ) //Error connecting to server return nError; ....... } From the documentation I know I can use GetExitCodeThread to get the error code but where is the best place to put this? Should I post an exit message to the main thread and read the code there? Are there any other solutions that would be better? Thanks.

    L 1 Reply Last reply
    0
    • M masnu

      I have created a worker thread to connect to a data server. The thread needs to wait a few seconds and exit if no data arrives (i.e. server is not online). I need to trap the thread exit code so my main thread can know if the workher thread terminated with an error. I have the following code: static UINT Connect(void* p_this); UINT Connect(); UINT Client::Connect(void* p_this) { Client* pClient = (Client*)p_this; pClient->Connect(); } UINT Client::Connect() { CDataServer pServer = new CDataServer(); ........ int nError = pServer->Connect(); if ( nError != 0 ) //Error connecting to server return nError; ....... } From the documentation I know I can use GetExitCodeThread to get the error code but where is the best place to put this? Should I post an exit message to the main thread and read the code there? Are there any other solutions that would be better? Thanks.

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      masnu wrote:

      so my main thread can know if the workher thread terminated with an error.

      What error? Use of thread exit codes are most often replaced with a form of communication of your own design. You are doing Object Oriented Programming aren't you?

      led mike

      M 1 Reply Last reply
      0
      • L led mike

        masnu wrote:

        so my main thread can know if the workher thread terminated with an error.

        What error? Use of thread exit codes are most often replaced with a form of communication of your own design. You are doing Object Oriented Programming aren't you?

        led mike

        M Offline
        M Offline
        masnu
        wrote on last edited by
        #3

        The exit code I want is the one I have defined as nError in the above example. Based on that number I can determine why the thread exited.

        E CPalliniC L 3 Replies Last reply
        0
        • M masnu

          The exit code I want is the one I have defined as nError in the above example. Based on that number I can determine why the thread exited.

          E Offline
          E Offline
          Ernest Laurentin
          wrote on last edited by
          #4

          From your code, I can say that you need: 1. return the error code in your thread function Connect(void* param) 2. From the main thread you can call GetExitCodeThread Better solution is something like Mike suggested, define an interface to receive the error or success is much better.

          God bless, Ernest Laurentin

          1 Reply Last reply
          0
          • M masnu

            The exit code I want is the one I have defined as nError in the above example. Based on that number I can determine why the thread exited.

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Your assumption is correct. You can return nError and then read that number via GetExitCodeThread from the main thread. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • M masnu

              The exit code I want is the one I have defined as nError in the above example. Based on that number I can determine why the thread exited.

              L Offline
              L Offline
              led mike
              wrote on last edited by
              #6

              *groan*

              led mike

              M 1 Reply Last reply
              0
              • L led mike

                *groan*

                led mike

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                O M G :((

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                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