win 32 多线程 2 GetExltCodeThread

// ExitCode.c

#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;

 hThrd1 = CreateThread(NULL,
  0,
  ThreadFunc,
  (LPVOID)1,
  0,
  &threadId);
 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 untill both calls to
 // GetExitCodeThread succeed and
 // Neither of them returns STILL_ACTIVE
 for(;;)
 {
  printf("press any key to exit/n");
  getch();

  int n;
  n = GetExitCodeThread(hThrd1, &exitCode1);
  n = GetExitCodeThread(hThrd2, &exitCode2);
  if(exitCode1 == STILL_ACTIVE)
  {
   puts("thread 1 is still running/n");
  }
  if(exitCode2 == STILL_ACTIVE)
  {
   puts("thread 2 is still running/n");
  }
  if(exitCode1 != STILL_ACTIVE && exitCode2 != STILL_ACTIVE)
  {
   break;
  }
 }

 CloseHandle(hThrd1);
 CloseHandle(hThrd2);

 printf("thread 1 returned %d/n", exitCode1);
 printf("thread 2 returned %d/n", exitCode2);

 return EXIT_SUCCESS;
}

 


DWORD WINAPI ThreadFunc(LPVOID n)
{
 Sleep((DWORD)n*10000*2);

 return (DWORD)n*10;
}

 

 

/*********************************************************************************
BOOL GetExltCodeThread(
 HANDLE hThread,
 LPDWORD lpExitCode
);
hThread   由CreateThread()传回的线程handle
lpExitCode  指向DWORD用于接受结束代码
返回值:
 如果成功返回TRUE否则FALSE    调用GetLastError()找出原因
 如果线程已经结束,那么结束代码lpExitCode参数中带回来。
 如果线程尚未结束,lpExitCode带回来的值是STILL_ACTIVE
*********************************************************************************/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值