TerminateThread为何结束不了线程呢
TerminateThread为什么结束不了线程呢
------解决方案--------------------
参考MSDN
Warning If a thread happens to return STILL_ACTIVE (259) as an error code, applications that test for this value could end up in an infinite loop
------解决方案--------------------
我测试了下,结果的确是这样的,但2楼的回答貌似解决了楼主的问题了
DWORD WINAPI ThreadFun(LPVOID p)
{
while(1)
{
///printf(" 子线程running \n");
Sleep(1000);
}
printf("线程结束了\n");
return 0;
}
int main()
{
typedef unsigned int (_stdcall *ThreadType )(LPVOID);
HANDLE hThread=(HANDLE)_beginthreadex(NULL,0,(ThreadType)ThreadFun,NULL,0,NULL);
printf("主线程 runing \n");
Sleep(500);
char c;
while( (c=getchar()) !='a');
//TerminateThread(HANDLE(944),1111);
TerminateThread(hThread,0);
if( GetExitCodeThread(hThread,&ExitCode) )
{
if(ExitCode==STILL_ACTIVE)
{
printf("子线程没有结束\n"); //这里可以打印出结果来,怎么回事
}
}
printf("主线程 dead \n");
CloseHandle(hThread);
return 0;
}
------解决方案--------------------
参考MSDN
Warning If a thread happens to return STILL_ACTIVE (259) as an error code, applications that test for this value could end up in an infinite loop
------解决方案--------------------
我测试了下,结果的确是这样的,但2楼的回答貌似解决了楼主的问题了