Getting Thread Exit Code
-
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 useGetExitCodeThread
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. -
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 useGetExitCodeThread
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.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
-
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
-
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.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 callGetExitCodeThread
Better solution is something like Mike suggested, define an interface to receive the error or success is much better.God bless, Ernest Laurentin
-
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.Your assumption is correct. You can return
nError
and then read that number viaGetExitCodeThread
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 -
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. -
O M G :((
Mark Salsbery Microsoft MVP - Visual C++ :java: