#include<windows.h> #include<iostream> using namespace std; /* the threadPro is over which stand for the pid is invalid GetCurrentThread 返回虚拟句柄,pid是系统唯一标示线程进程的轻量级表示符号 线程的 ID 是唯一的; 而句柄可能不只一个, 譬如可以用 GetCurrentThread 获取一个伪句柄、可以用 DuplicateHandle 复制一个句柄等等. OpenThread() 根绝PID获得线程句柄 一句话,当Event此时置位,并且waite在自己等待时间范围内一直阻塞,waite返回后如果没有reset那么event还是置位的 CreateEvent(SECURITY_ATTRIBUTES,//在同一个进程内设置为NULL fManual,是否手动ResetEevent 设置为false 意思是自动 设置true 手动。手动要在SetEvent,WaiteForSignaleObject返回后手动复位。如果没复位在一直是置为的,后面的对于后面的waite.还是有效的 fInitial,是否初始化,当为true的时候意思是初始化了,置位了。这时候第一个WaiteForSingleObject直接返回了。FALSE意思是,Eevent没有置位相当是ResetEvent. pszName. http://support.microsoft.com/kb/127992/zh-cn 每个执行的线程都有与其相关的挂起计数。如果这个计数为0,那么不会挂起线程。如果为非0值,则线程就会处于挂起状态。每次调用SuspendThread()都会增加这个计数。每次调用ResumeThread()都会减小这个挂起计数。挂起的线程只有在它的挂起计数达到0时才会恢复。因此,为了恢复一个挂起的线程,对ResumeThread()的调用次数必须与对SuspendThread()的调用次数相等。 这两个函数都返回线程先前的挂起计数,如果发生错误,返回值为–1。 http://blog.csdn.net/powerlly/article/details/4189010 */ CONSOLE_SCREEN_BUFFER_INFO cConInfo; HANDLE hWndConsole = NULL; HANDLE hEvent; DWORD WINAPI fnTheadPrintCharPro(LPVOID lpParam) { COORD* pos = reinterpret_cast<COORD*>(lpParam); GetConsoleScreenBufferInfo(hWndConsole,&cConInfo); DWORD dSize = 0; LPCSTR lpszPrintChar = "hello,word"; printf("not real handle %d \n",GetCurrentThread()); SetConsoleCursorPosition(hWndConsole,*pos); for(int iRowIndex = 0;iRowIndex < 50; iRowIndex++ ) { pos->X = pos->X + 1 ; pos->Y = pos->Y + 1 ; // WriteConsoleOutputCharacter(hWndConsole,lpszPrintChar,strlen(lpszPrintChar),*pos,&dSize); printf("not real handle %d \n",GetCurrentThread()); } pos->X += 1; pos->Y = 0; SetConsoleCursorPosition(hWndConsole,*pos); SetEvent(hEvent); return 0;//success } DWORD WINAPI ThreadPrintNumberProc(LPVOID lpParam) { DWORD dwSize; COORD* coPos = reinterpret_cast<COORD*>(lpParam); GetConsoleScreenBufferInfo(hWndConsole,&cConInfo); LPSTR lpszNumber = "1234567891"; SetConsoleCursorPosition(hWndConsole,*coPos); for(int iRowIndex = 50;iRowIndex >= 0;iRowIndex--) { coPos->X -= 1; coPos->Y -= 1; // WriteConsoleOutputCharacter(hWndConsole,lpszNumber,strlen(lpszNumber),*coPos,&dwSize); printf("not real %d \n",GetCurrentThread()); } coPos->X += 1; coPos->Y = 0; SetConsoleCursorPosition(hWndConsole,*coPos); SetEvent(hEvent); return 0; } int main() { TCHAR lpszDebug[200]; DWORD dPid; DWORD dwNumberPid; HANDLE hWndPrintChar = NULL; HANDLE hWndPrintNumber = NULL; COORD posCharacter; COORD posNumber; hEvent = CreateEvent(NULL,TRUE,TRUE,NULL); hWndConsole = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(hWndConsole,&cConInfo); posCharacter.X = 0; posCharacter.Y = 0; posNumber.X = cConInfo.dwMaximumWindowSize.Y; posNumber.Y = cConInfo.dwMaximumWindowSize.Y; hWndPrintChar = CreateThread(NULL,0,fnTheadPrintCharPro,&posCharacter,CREATE_SUSPENDED,&dPid); hWndPrintNumber = CreateThread(NULL,0,ThreadPrintNumberProc,&posNumber,CREATE_SUSPENDED,&dwNumberPid); if(hWndPrintChar == NULL || hWndPrintNumber == NULL) { sprintf(lpszDebug,"the error code is %d\n",GetLastError()); OutputDebugString(lpszDebug); } printf("the Thread %d is create_suspended\n",dPid); ResumeThread(hWndPrintChar); ResetEvent(hEvent); WaitForSingleObject(hEvent,INFINITE); printf("Char is over\n"); ResumeThread(hWndPrintNumber); printf("the thread %d is start\n"); WaitForSingleObject(hEvent,INFINITE); printf("thread %d is over\n"); CloseHandle(hWndPrintChar); CloseHandle(hWndPrintNumber); system("pause"); return 0; }