vc 控制台下 窗口消息处理 以及 定时函数 SetTimer

控制台是没有窗口处理队列的,所以必须自己处理函数。

 

进程间的通信。用 #define WM_USER_MSG WM_USER+102

WM_COPYDATA貌似不起作用。

 

我们手动制作一个消息处理函数,然后就可以进行 settimer,定时处理任务了。

 

// HOOK 消息循环处理.cpp : 定义控制台应用程序的入口点。 //

#include "StdAfx.h" #include  <windows.h> 

unsigned  long  WINAPI  Thread(PVOID  pvoid); 

DWORD  dwThreadId;  #define WM_USER_MSG WM_USER+102

void  main()  {     printf("use timer in workthread of console application<masterz>\n");   HANDLE  hThread  =  CreateThread(     NULL,       //  no  security  attributes     0,        //  use  default  stack  size      Thread,       //  thread  function     0,        //  argument  to  thread  function     0,        //  use  default  creation  flags     &dwThreadId);    //DWORD  dwwait=WaitForSingleObject(hThread,1000*1);   //switch(dwwait)   //{   //case  WAIT_ABANDONED:   // printf("main  thread  WaitForSingleObject  return  WAIT_ABANDONED\n");   // break;   //case  WAIT_OBJECT_0:   // printf("main  thread  WaitForSingleObject  return  WAIT_OBJECT_0\n");   // break;   //case  WAIT_TIMEOUT:   // printf("main  thread  WaitForSingleObject  return  WAIT_TIMEOUT\n");   // break;   //}   Sleep(1000);  PostThreadMessage(dwThreadId,WM_USER_MSG,NULL,NULL);  CloseHandle(hThread);   getchar();  } 

unsigned long WINAPI Thread(PVOID  pvoid)  {   MSG  msg;     //PeekMessage(&msg,  NULL,  WM_USER,  WM_USER,  PM_NOREMOVE);     UINT  timerid=SetTimer(NULL,111,3000,NULL);   BOOL  bRet;   int  count  =0;   while(  (bRet  =  GetMessage(  &msg,  NULL,  0,  0  ))  !=  0)   {     if  (bRet  ==  -1)    {     //  handle  the  error  and  possibly  exit    }    else    {    if(msg.message== WM_USER_MSG)    {     printf("消息来了");    };    if(msg.message==WM_TIMER)     {      count++;      printf("WM_TIMER  in  work  thread  count=%d\n",count);      if(count>100)       break;     }     else     {      TranslateMessage(&msg);       DispatchMessage(&msg);      }    }  }  // KillTimer(NULL,timerid);   printf("thread  end  here\n");   return  0;  }

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值