Closed AiXanadu closed 2 years ago
Sorry but this doesn't work as expected. I tried and it immediately killed the thread instead of just checking it's status. If have also seen people trying to use GetExitCodeThread
to test if a thread is still running, but actually MS is pointing out not to do it, and instead suggest to use WaitForSingleObject
with 0 duration, what the current code is already doing:
Important The GetExitCodeThread function returns a valid error code defined by the application only after the thread terminates. Therefore, an application should not use STILL_ACTIVE (259) as an error code. If a thread returns STILL_ACTIVE (259) as an error code, applications that test for this value could interpret it to mean that the thread is still running and continue to test for the completion of the thread after the thread has terminated, which could put the application into an infinite loop. To avoid this problem, callers should call the GetExitCodeThread function only after the thread has been confirmed to have exited. Use the WaitForSingleObject function with a wait duration of zero to determine whether a thread has exited.
Sometimes WaitForSingleObject cannot be returned due to thread exceptions. TerminateThread can force the thread to end.