James
James

Reputation: 4052

Visual C++ function not seeing thread termination

I have the following code in an onClick event function for my form:

HANDLE hThread;
unsigned threadID;

output->Text = "Starting";
_beginthreadex( NULL, 0, &start, NULL, 0, &threadID );
DWORD dwRes = WaitForSingleObject( hThread, INFINITE );

if (dwRes == WAIT_OBJECT_0 || dwRes == WAIT_ABANDONED || dwRes == WAIT_TIMEOUT){
CloseHandle( hThread );
output->Text = "Done";
}

The function start returns 0 whatever happens. I'm seeing output label change to "Starting", but it never changes to "Done". However, I see the following message in the Visual Studio output window:

The thread 'Win32 Thread' (0x2dc) has exited with code 0 (0x0).
The thread '<No Name>' (0x2dc) has exited with code 0 (0x0).

I'm not sure why the if statement isn't catching the end of the thread. Is there any function I can use with the thread handle to probe the state of the thread?

Upvotes: 0

Views: 32

Answers (1)

Gautam Jain
Gautam Jain

Reputation: 6849

You haven't assigned the thread handle to hThread.

It should be:

hThread = _beginthreadex( NULL, 0, &start, NULL, 0, &threadID );

Upvotes: 2

Related Questions