📄 subject_64068.htm
字号:
<p>
序号:64068 发表者:badboy 发表日期:2003-12-08 16:49:35
<br>主题:线程的结束代码是什么意思?
<br>内容:/*GetExitCodeThread(hThrd,&exitcode);你们用过吗?线程结束代码是不是就一个线程最终返回的那个值?大家帮我试下下面的那个程序,感觉程序的结果很怪,不知道,为什么结果总有可能是 259 而且,我把线程函数里的那个间隔改成很多数,结果也总有可能是259 ,真是奇怪!*/<BR> <BR>#include <stdio.h><BR>#include <stdlib.h><BR>#include <windows.h><BR>#include <iostream.h><BR>HANDLE hevent;<BR>DWORD shijian;<BR>DWORD shijian1;<BR>DWORD WINAPI ThreadFunc(LPVOID);<BR>DWORD exitcode=0;<BR>int main()<BR>{<BR> HANDLE hThrd;<BR> DWORD threadId;<BR> hevent=CreateEvent(NULL,FALSE,FALSE,NULL);<BR> hThrd = CreateThread(NULL,<BR> 0,<BR> ThreadFunc,<BR> (LPVOID)3,<BR> 0,<BR> &threadId );<BR> if(WaitForSingleObject(hevent,INFINITE)==WAIT_OBJECT_0)<BR> {<BR> cout<<"i am main thread ,sub thread is over!";<BR> GetExitCodeThread(hThrd,&exitcode);<BR> cout<<"and sub thread's result is :"<<exitcode<<endl;<BR> <BR> }<BR> CloseHandle(hThrd);<BR> return EXIT_SUCCESS;<BR>}<BR>DWORD WINAPI ThreadFunc(LPVOID n)<BR>{<BR> shijian=GetTickCount();<BR> int i=0;<BR> do<BR> {<BR> cout<<"ok,this thread is running!"<<endl;<BR> i++;<BR> }while((GetTickCount()-shijian)<=1000); <BR> //上面这句代码可以更改时间间隔!<BR> SetEvent(hevent);<BR> return i;<BR>}<BR>
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:badboy 回复日期:2003-12-09 16:35:22
<br>内容:顶!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:hurtmanzc 回复日期:2003-12-09 21:03:22
<br>内容:<BR>Remarks<BR>If the specified thread has not terminated, the termination status returned is STILL_ACTIVE. If the thread has terminated, the termination status returned may be one of the following: <BR><BR>The exit value specified in the ExitThread or TerminateThread function. <BR>The return value from the thread function. <BR>The exit value of the thread's process.
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:badboy 回复日期:2003-12-10 17:28:20
<br>内容:高手,帮我再看! <BR> 按您给的资料,线程结束代码的意思不是线程最后计算的结果,而是一种状态值吗?<BR> DWORD WINAPI ThreadFunc(LPVOID n)<BR>{<BR> shijian=GetTickCount();<BR> int i=0;<BR> do<BR> {<BR> cout<<"ok,this thread is running!"<<endl;<BR> i++;<BR> }while((GetTickCount()-shijian)<=1000); <BR> //上面这句代码可以更改时间间隔!<BR> SetEvent(hevent);<BR> return i;<BR>}<BR> 上面这个线程函数返回的i值是不是和<BR> GetExitCodeThread(hThrd,&exitcode); 函数返回的exitcode是一个值?<BR><BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:hurtmanzc 回复日期:2003-12-10 21:31:51
<br>内容:就是!你的程序属于第二种情况。<BR>The return value from the thread function
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:badboy 回复日期:2003-12-17 22:02:10
<br>内容:那请问第三种情况是什么意思?<BR>The exit value of the thread's process. <BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:hurtmanzc 回复日期:2003-12-17 23:04:15
<br>内容:GetExitCodeProcess<BR>The GetExitCodeProcess function retrieves the termination status of the specified process. <BR><BR>BOOL GetExitCodeProcess(<BR> HANDLE hProcess, // handle to the process<BR> LPDWORD lpExitCode // address to receive termination status<BR>);<BR> <BR>Parameters<BR>hProcess <BR>Handle to the process. <BR>Windows NT: The handle must have PROCESS_QUERY_INFORMATION access. <BR><BR>lpExitCode <BR>Pointer to a 32-bit variable to receive the process termination status. <BR>Return Values<BR>If the function succeeds, the return value is nonzero.<BR><BR>If the function fails, the return value is zero. To get extended error information, call GetLastError. <BR><BR>Remarks<BR><BR>If the specified process has not terminated, the termination status returned is STILL_ACTIVE. If the process has terminated, the termination status returned may be one of the following: <BR><BR>The exit value specified in the ExitProcess or TerminateProcess function. <BR>The return value from the main or WinMain function of the process. <BR>The exception value for an unhandled exception that caused the process to terminate. <BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:badboy 回复日期:2003-12-18 10:18:18
<br>内容:高手,上面您说的是进程的结束代码。<BR><BR>您的意思是不是说,<BR>The exit value of the thread's process.<BR><BR> 这句话的意思是线程的返回值可能是进程的结束代码?也就是主线程先结束的话,那么每个分线程的结束代码就是主线程的结束代码? 这种情况,我们一般不会让他出现吧? 因为,主线程总会想办法等待分线程的结束,或者是强行结束他们TerminateThread ()。而不会是主线程执行完了就不管了,而任由各分线程自己执行到哪就算哪!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -