我的表单的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
语句没有抓住线程的结尾。我可以使用任何函数与线程句柄来探测线程的状态吗?
答案 0 :(得分:2)
您尚未将线程句柄分配给hThread。
应该是:
hThread = _beginthreadex( NULL, 0, &start, NULL, 0, &threadID );