16,466
社区成员
发帖
与我相关
我的任务
分享
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#define WM_MYMESSAGE (WM_USER + 1)
unsigned long WINAPI Thread(PVOID pvoid);
HWND GetConsoleHwnd(void)
{
#define MY_BUFSIZE 1024 // Buffer size for console window titles.
HWND hwndFound; // This is what is returned to the caller.
char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated
// WindowTitle.
char pszOldWindowTitle[MY_BUFSIZE]; // Contains original
// WindowTitle.
// Fetch current window title.
GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
// Format a "unique" NewWindowTitle.
wsprintf(pszNewWindowTitle,"%d/%d",
GetTickCount(),
GetCurrentProcessId());
// Change current window title.
SetConsoleTitle(pszNewWindowTitle);
// Ensure window title has been updated.
Sleep(40);
// Look for NewWindowTitle.
hwndFound=FindWindow(NULL, pszNewWindowTitle);
// Restore original window title.
SetConsoleTitle(pszOldWindowTitle);
return(hwndFound);
}
void main()
{
DWORD dwThreadId;
printf( "use timer in workthread of console application <masterz> \n ");
/*DWORD dwwait=WaitForSingleObject(hThread,1000*30);
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;
}
CloseHandle(hThread); */
HWND hwnd = GetConsoleHwnd();
HANDLE hThread=CreateThread(NULL,0,Thread,&hwnd,0,&dwThreadId);
while (true)
{
::SendMessage(hwnd,WM_MYMESSAGE,0,0);
Sleep(1000);
}
_getch();
}
unsigned long WINAPI Thread(PVOID pvoid)
{
MSG msg;
int count =0;
PeekMessage(&msg,*(HWND*)pvoid,WM_MYMESSAGE,WM_MYMESSAGE,PM_NOREMOVE);
//UINT timerid = SetTimer(NULL,111,3000,NULL);
while(1)
{
GetMessage(&msg,NULL,0,0);
// if(msg.message==WM_TIMER)
// {
// count++;
// printf( "WM_TIMER in work thread count=%d\n ",count);
// //if(count> 4)
// KillTimer(NULL,timerid); //break;
// }
if (msg.message==WM_MYMESSAGE)
{
printf("收到了自己的消息了,哈哈");
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
/*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
printf("handle the error and possibly exit \n");
}
else
if(msg.message==WM_TIMER)
{
count++;
printf( "WM_TIMER in work thread count=%d\n ",count);
if(count> 4)
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
KillTimer(NULL,timerid);
printf( "thread end here\n "); */
return 0;
}
#include "stdafx.h"
#include <atlstr.h>
//#include <debug.h>
//using namespace UTIL_LFS09;
bool g_exit=false;
DWORD g_dwMainThreadID;
#define WM_MY WM_USER+11
DWORD index=0;
DWORD dwThreadId;
void Thread(PVOID pvoid);
BOOL WINAPI ConsoleCtrlhandler(DWORD dwCtrlType)
{
if (dwCtrlType==CTRL_CLOSE_EVENT)
{
PostThreadMessage(g_dwMainThreadID,WM_CLOSE,0,0);
return TRUE;
}
return FALSE;
}
int main(int argc, char *argv[])
{
g_dwMainThreadID=GetCurrentThreadId();
SetConsoleCtrlHandler(ConsoleCtrlhandler,TRUE);
HANDLE hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Thread,&g_dwMainThreadID,0,&dwThreadId);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
switch(msg.message)
{
case WM_MY:
printf("get custom message %d\r\n",msg.wParam);
break;
case WM_CLOSE:
g_exit=true;
PostThreadMessage(dwThreadId,WM_CLOSE,0,0);
PostThreadMessage(g_dwMainThreadID,WM_QUIT,0,0);
break;;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
void Thread(PVOID pvoid)
{
MSG msg;
SetTimer(NULL,11,1000,0);
while(GetMessage(&msg,NULL,0,0))
{
switch(msg.message)
{
case WM_TIMER:
PostThreadMessage(*(DWORD*)pvoid,WM_MY,index++,0);
break;
case WM_CLOSE:
KillTimer(NULL,11);
printf("get sub thread close \r\n");
PostThreadMessage(dwThreadId,WM_QUIT,0,0);
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
hwndFound;
DWORD dwThreadId;
HWND GetConsoleHwnd(void)
{
#define MY_BUFSIZE 1024 // Buffer size for console window titles.
// This is what is returned to the caller.
char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated
// WindowTitle.
char pszOldWindowTitle[MY_BUFSIZE]; // Contains original
// WindowTitle.
// Fetch current window title.
GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
// Format a "unique" NewWindowTitle.
wsprintf(pszNewWindowTitle,"%d/%d",
GetTickCount(),
GetCurrentProcessId());
// Change current window title.
SetConsoleTitle(pszNewWindowTitle);
// Ensure window title has been updated.
Sleep(40);
// Look for NewWindowTitle.
hwndFound=FindWindow(NULL, pszNewWindowTitle);
// Restore original window title.
SetConsoleTitle(pszOldWindowTitle);
return(hwndFound);
}
void main()
{
hwndFound = GetConsoleHwnd();
HANDLE hThread=CreateThread(NULL,0,Thread,0,0,0);
MSG msg;
dwThreadId = GetCurrentThreadId();
while(GetMessage(&msg,NULL,0,0))
{
if (msg.message==WM_MYMESSAGE)
{
OutputDebugString("GetMessage");
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
unsigned long WINAPI Thread(PVOID pvoid)
{
while (true)
{
//::SendMessage(hwndFound,WM_MYMESSAGE,0,0);
PostThreadMessage(dwThreadId,WM_MYMESSAGE,0,0);
printf("DD\n");
Sleep(1000);
}
return 0;
}
HANDLE hThread=CreateThread(NULL,0,Thread,&hwnd,0,&dwThreadId);
while (true)
{
::SendMessage(*(HWND*)pvoid,WM_MYMESSAGE,0,0);
printf("DD\n");
Sleep(1000);
}