面对现实,超越自己
逆水行舟,不进则退
posts - 269,comments - 32,trackbacks - 0

GetExitCodeThread函数是获得线程的退出码, 

函数: GetExitCodeThread()

功能:获取一个结束线程的返回值

函数原形: BOOL GetExitCodeThread( HANDLE hThread, LPDWORD lpExitCode);

参数: hThread 指向欲获取返回值的线程对象的句柄

               lpExitCode 用于存储线程的返回值

返回值:函数执行成功则返回非0值,否则返回 0FALSE

第一个参数是线程句柄,用 CreateThread 创建线程时获得到。

第二个参数是一个 DWORD的指针,用户应该使用一个 DWORD 类型的变量去接收数据,返回的数据是线程的退出码,
通过线程退出码可以判断线程是否正在运行,还是已经退出。或者可以判断线程是否是正常退出还是异常退出。

执行成功时,存放线程的状态码,如果是线程的返回值,表示线程执行完,  如果线程没执行完,返回STILL_ACTIVE,如果线程的返回值就是STILL_ACTIVE,就无法判断  .


MSDN解释:

GetExitCodeThread Function

Retrieves the termination status of the specified thread.

 

BOOL WINAPI GetExitCodeThread(   __in           HANDLE hThread,    __out         LPDWORD lpExitCode ); 

Parameters

hThread

A handle to the thread.

The handle must have the THREAD_QUERY_INFORMATION access right. For more information, see Thread Security and Access Rights.

lpExitCode

A pointer to a variable to receive the thread termination status. If the specified thread has not terminated and the function succeeds, the termination status returned is STILL_ACTIVE.

Return Value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

If the thread has terminated and the function succeeds, the termination status returned may be one of the following:

 

  • The exit value specified in the ExitThread or TerminateThread function.
  • The return value from the thread function.
  • The exit value of the thread's process.

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.


参考例子:
 1 int main()     
 2 {         
 3     DWORD exitCode1 = 0;     
 4     DWORD exitCode2 = 0;     
 5     DWORD threadId;          
 6   
 7     HANDLE hThrd1 = CreateThread(NULL, 0,   ThreadFunc1, 0, 0, &threadId );  
 9     if (hThrd1)     
10         printf("Thread 1 launched\n");    
11 
13     HANDLE hThrd2 = CreateThread(NULL, 0,  ThreadFunc2, 0, 0, &threadId );     
14     if (hThrd2)     
15         printf("Thread 2 launched\n");    
16 
18     for (;;)      
19     {   
20           printf("Press any key to exit..\n");   
21           getch();   
22           GetExitCodeThread(hThrd1, &exitCode1);   
23           GetExitCodeThread(hThrd2, &exitCode2);   
24           if ( exitCode1 == STILL_ACTIVE )   
25               puts("Thread 1 is still running!");   
26   
27           if ( exitCode2 == STILL_ACTIVE )   
28               puts("Thread 2 is still running!");   
29           if ( exitCode1 != STILL_ACTIVE   && exitCode2 != STILL_ACTIVE )   
30               break;   
31     }   
32   
33     CloseHandle(hThrd1);   
34     CloseHandle(hThrd2);   
35    
36     printf("Thread 1 returned %d\n", exitCode1);   
37     printf("Thread 2 returned %d\n", exitCode2);   
38     return EXIT_SUCCESS;     
39 }    
40   
41 DWORD WINAPI ThreadFunc1(LPVOID n)   
42 {   
43      Sleep((DWORD)n*1000*2);   
44      return (DWORD)n * 10;   
45 } 
46 
48 DWORD WINAPI ThreadFunc2(LPVOID n)   
49 {   
50      Sleep((DWORD)n*1000*2);   
51      return (DWORD)n * 10;   
52 } 
posted on 2013-07-31 11:37 王海光 阅读(5658) 评论(0)  编辑 收藏 引用 所属分类: MFC

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理