控制台是没有窗口处理队列的,所以必须自己处理函数。
进程间的通信。用 #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;
}