Win32 线程

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值