Visual C ++函数没有看到线程终止

时间:2014-06-25 09:33:10

标签: multithreading visual-studio-2010 visual-c++

我的表单的onClick事件函数中有以下代码:

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";
}

无论发生什么,函数start都会返回0。我看到output标签更改为“正在启动”,但它永远不会更改为“完成”。但是,我在Visual Studio输出窗口中看到以下消息:

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

我不确定为什么if语句没有抓住线程的结尾。我可以使用任何函数与线程句柄来探测线程的状态吗?

1 个答案:

答案 0 :(得分:2)

您尚未将线程句柄分配给hThread。

应该是:

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