BOOL GetExitCodeThread (
HANDLE hThread, // in,线程handle,也就是CreateThread()的返回值
LPDWORD lpExitCode //out,存储线程结束代码,也就是线程的返回值
);
说明: 此函数调用成功返回TRUE,失败返回FALSE,只表示这个函数是否调用成功而己.
不能根据返回值来判断一个线程是否结束,而要根据 lpExitCode的值来确定,
lpExitCode 值STILL_ACTIVE 表示线程正在运行.
若线程己经结束,则lpExitCode中存储指定线程的返回值.
例:
/* * ExitCode.c * * Sample code for "Multithreading Applications in Win32" * This is from Chapter 2, Listing 2-2 * * Start two threads and try to exit * when the user presses a key. */
#define WIN32_LEAN_AND_MEAN#include <stdio.h>#include <stdlib.h>#include <windows.h>#include <conio.h>
DWORD WINAPI ThreadFunc(LPVOID);
int main(){ HANDLE hThrd1; HANDLE hThrd2; DWORD exitCode1 = 0; DWORD exitCode2 = 0; DWORD threadId;
//创建成功,返回一个HANDLE,对于此线程的操作函数,几乎都使用此HANDLE hThrd1 = CreateThread(NULL, 0, //堆栈大小,0取默认值1MB ThreadFunc, //线程函数地址 (LPVOID)1, //线程的传入参数 0, //0 表示线程一旦创建 立即执行 &threadId ); //线程ID,一般为 向线程发送消息时用, if (hThrd1) printf("Thread 1 launched/n");
// hThrd2 = CreateThread(NULL, 0, ThreadFunc, (LPVOID)2, 0, &threadId ); if (hThrd2) printf("Thread 2 launched/n");
// Keep waiting until both calls to // GetExitCodeThread succeed AND // neither of them returns STILL_ACTIVE. // This method is not optimal - we'll // see the correct way in Chapter 3. for (;;) { printf("Press any key to exit../n"); getch(); // 按任意键继续
// 刺探 指定线程(通过线程的HANDLE) 是否己经结束, //exitCode1中存储线程状态,若正在运行,值为STILL_ACTIVE //若己经结束,值为 线程的返回值(即 return 后的值) GetExitCodeThread(hThrd1, &exitCode1); GetExitCodeThread(hThrd2, &exitCode2); if ( exitCode1 == STILL_ACTIVE ) puts("Thread 1 is still running!"); if ( exitCode2 == STILL_ACTIVE ) puts("Thread 2 is still running!"); if ( exitCode1 != STILL_ACTIVE && exitCode2 != STILL_ACTIVE ) break; }
CloseHandle(hThrd1); //线程核心对象的引用计数 减1 CloseHandle(hThrd2);
printf("Thread 1 returned %d/n", exitCode1); printf("Thread 2 returned %d/n", exitCode2);
return EXIT_SUCCESS;}
/* * Take the startup value, do some simple math on it, * and return the calculated value. */DWORD WINAPI ThreadFunc(LPVOID n){ Sleep((DWORD)n*1000*2); return (DWORD)n * 10;}
//以上代码,存储进text文件,以.c为扩展名,拖进VC 6.0,编译,运行.
本文来自博客,转载请标明出处:http://blog.csdn.net/night_cat/archive/2009/05/29/4223462.aspx