Retrieving the exit code of CWinThread objects

This is a discussion on Retrieving the exit code of CWinThread objects within the Windows Programming forums, part of the Platform Specific Boards category; from msdn: " Retrieving the Exit Code of a Thread To get the exit code of either the worker or ...

  1. #1
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251

    Retrieving the exit code of CWinThread objects

    from msdn:
    "
    Retrieving the Exit Code of a Thread
    To get the exit code of either the worker or the user-interface thread, call the GetExitCodeThread function.
    ..

    Retrieving the exit code of CWinThread objects takes an extra step. By default, when a CWinThread thread terminates, the thread object is deleted. This means you cannot access the m_hThread data member because the CWinThread object no longer exists. To avoid this situation, do one of the following:

    Set the m_bAutoDelete data member to FALSE. This allows the CWinThread object to survive after the thread has been terminated. You can then access the m_hThread data member after the thread has been terminated. If you use this technique, however, you are responsible for destroying the CWinThread object because the framework will not automatically delete it for you. This is the preferred method.
    "
    Question: If the CWinThread was allocated by new I will call the destructor with delete. But how will I call the destructor if:
    (1) CWinThread is allocated as a function variable?
    (2) CWinThread is a global variable?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,649
    AfxBeginThread creates the CWinThread object, so (1) and (2) do not apply.
    http://msdn.microsoft.com/en-us/libr...z9(VS.80).aspx

    >> Set the m_bAutoDelete data member to FALSE.
    Keep in mind that in order to do this safely, you must create the thread suspended, set the flag, then resume the thread.

    gg

  3. #3
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by Codeplug View Post
    AfxBeginThread creates the CWinThread object, so (1) and (2) do not apply.
    http://msdn.microsoft.com/en-us/libr...z9(VS.80).aspx

    >> Set the m_bAutoDelete data member to FALSE.
    Keep in mind that in order to do this safely, you must create the thread suspended, set the flag, then resume the thread.

    gg
    why does Microsoft makes thing so programmer unfriendly??
    It seems to be a real mess

  4. #4
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by Codeplug View Post
    AfxBeginThread creates the CWinThread object, so (1) and (2) do not apply.
    http://msdn.microsoft.com/en-us/libr...z9(VS.80).aspx

    >> Set the m_bAutoDelete data member to FALSE.
    Keep in mind that in order to do this safely, you must create the thread suspended, set the flag, then resume the thread.

    gg
    What you say doesn't seem to be correct cause instead is pointed by Microsoft as the second solution
    here is the full extract


    Retrieving the Exit Code of a Thread
    To get the exit code of either the worker or the user-interface thread, call the GetExitCodeThread function. For information about this function, see the Platform SDK. This function takes the handle to the thread (stored in the m_hThread data member of CWinThread objects) and the address of a DWORD.

    If the thread is still active, GetExitCodeThread will place STILL_ACTIVE in the supplied DWORD address; otherwise, the exit code is placed in this address.

    Retrieving the exit code of CWinThread objects takes an extra step. By default, when a CWinThread thread terminates, the thread object is deleted. This means you cannot access the m_hThread data member because the CWinThread object no longer exists. To avoid this situation, do one of the following two things:

    Set the m_bAutoDelete data member to FALSE. This allows the CWinThread object to survive after the thread has been terminated. You can then access the m_hThread data member after the thread has been terminated. If you use this technique, however, you are responsible for destroying the CWinThread object as the framework will not automatically delete it for you. This is the preferred method.
    -or-

    Store the thread's handle separately. After the thread is created, copy its m_hThread data member (using :uplicateHandle) to another variable and access it through that variable. This way the object is deleted automatically upon termination and you can still find out why the thread terminated. Be careful that the thread does not terminate before you can duplicate the handle. The safest way to do this is to pass CREATE_SUSPENDED to AfxBeginThread, store the handle, and then resume the thread by calling ResumeThread.
    Either method allows you to determine why a CWinThread object terminated.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,649
    >> The safest way to do this is to pass CREATE_SUSPENDED to AfxBeginThread ...
    That goes for duplicating the handle or setting m_bAutoDelete to FALSE.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Odd memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 05-25-2006, 12:56 AM
  2. Replies: 1
    Last Post: 03-21-2006, 06:52 AM
  3. Replies: 60
    Last Post: 12-20-2005, 10:36 PM
  4. My Code won't exit
    By ZirDoX in forum C Programming
    Replies: 1
    Last Post: 01-28-2002, 04:43 AM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21