// Win32Thread.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
DWORD WINAPI TreadFunc(LPVOID lpParam)
{
int i = 0;
while (i < 20)
{
printf("I am from a thread, count = %d\n", i++);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hTread = NULL;
DWORD dwThreadId = 0;
hTread = ::CreateThread(NULL, 0, TreadFunc, NULL, 0, &dwThreadId);
printf("Now another thread has been created. ID=%d\n", dwThreadId);
::SuspendThread(hTread);
::SuspendThread(hTread);
getchar();
::ResumeThread(hTread);
getchar();
::ResumeThread(hTread);
DWORD dwExitCode;
if (::GetExitCodeThread(hTread, &dwExitCode))
{
if (dwExitCode == STILL_ACTIVE)
{
printf("thread still active\n");
}
else
{
printf("thread exit.\n");
}
}
::WaitForSingleObject(hTread, INFINITE);
if (::GetExitCodeThread(hTread, &dwExitCode))
{
if (dwExitCode == STILL_ACTIVE)
{
printf("thread still active\n");
}
else
{
printf("thread exit.\n");
}
}
::CloseHandle(hTread);
getchar();
return 0;
}
SuspendThread:线程暂停计数 + 1
ResumeThread:线程暂停计数 - 1,当减为0的时候,线程有机会跑
GetExitCodeThread函数可以查看线程是否还在跑。