用消息在Win32控制台程序多线程间进行通讯
2015-05-08 15:46
330 查看
#include <stdio.h>
#include <windows.h>
//#include <iostream>
//#include <process.h>
#define UM_MSG1 (WM_USER + 1)
#define UM_MSG2 (WM_USER + 2)
DWORD WINAPI Thread1(LPVOID para)
{
DWORD dwThreadId = *(DWORD *)para;
DWORD i = 0;
TCHAR *p;
char strTmp[100];
while(TRUE)
{
Sleep(1700);
p = new TCHAR[10];
sprintf_s(strTmp, 100, "Hello %d %x ", i++, p);
PostThreadMessage(dwThreadId, UM_MSG1, (WPARAM)strTmp, (LPARAM)p);
//delete []p;
}
return 0;
}
DWORD WINAPI Thread2(LPVOID para)
{
char strTmp[100];
DWORD dwThreadId = *(DWORD *)para;
DWORD i = 0;
TCHAR *p;
while(TRUE)
{
Sleep(3000);
p=new TCHAR[10];
sprintf_s(strTmp, 100, "World %d %x", i++, p);
PostThreadMessage(dwThreadId, UM_MSG2, (WPARAM)strTmp, (LPARAM)p);
//delete []p;
}
return 0;
}
int main()
{
printf("UM_MSG1: 0x%x/r/n", UM_MSG1);
printf("UM_MSG2: 0x%x/r/n", UM_MSG2);
DWORD dwValue = GetCurrentThreadId();
HANDLE hThread1 = CreateThread(NULL, 0, &Thread1, &dwValue, 0, NULL); // &Thread1可写作Thread1, 都是函数指针.
HANDLE hThread2 = CreateThread(NULL, 0, &Thread2, &dwValue, 0, NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
switch(msg.message)
{
case UM_MSG1:
case UM_MSG2:
printf("msg: 0x%x w: %x ws: %s l: %x/r/n", msg.message, &msg.wParam, msg.wParam, msg.lParam);
delete [](TCHAR *)msg.lParam; //注释掉这句你就会看到堆内存地址变化
break;
default:
printf("Unknown msg:0x%x/n",msg.message);
break;
}
Sleep(1);
}
return 0;
}
#include <windows.h>
//#include <iostream>
//#include <process.h>
#define UM_MSG1 (WM_USER + 1)
#define UM_MSG2 (WM_USER + 2)
DWORD WINAPI Thread1(LPVOID para)
{
DWORD dwThreadId = *(DWORD *)para;
DWORD i = 0;
TCHAR *p;
char strTmp[100];
while(TRUE)
{
Sleep(1700);
p = new TCHAR[10];
sprintf_s(strTmp, 100, "Hello %d %x ", i++, p);
PostThreadMessage(dwThreadId, UM_MSG1, (WPARAM)strTmp, (LPARAM)p);
//delete []p;
}
return 0;
}
DWORD WINAPI Thread2(LPVOID para)
{
char strTmp[100];
DWORD dwThreadId = *(DWORD *)para;
DWORD i = 0;
TCHAR *p;
while(TRUE)
{
Sleep(3000);
p=new TCHAR[10];
sprintf_s(strTmp, 100, "World %d %x", i++, p);
PostThreadMessage(dwThreadId, UM_MSG2, (WPARAM)strTmp, (LPARAM)p);
//delete []p;
}
return 0;
}
int main()
{
printf("UM_MSG1: 0x%x/r/n", UM_MSG1);
printf("UM_MSG2: 0x%x/r/n", UM_MSG2);
DWORD dwValue = GetCurrentThreadId();
HANDLE hThread1 = CreateThread(NULL, 0, &Thread1, &dwValue, 0, NULL); // &Thread1可写作Thread1, 都是函数指针.
HANDLE hThread2 = CreateThread(NULL, 0, &Thread2, &dwValue, 0, NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
switch(msg.message)
{
case UM_MSG1:
case UM_MSG2:
printf("msg: 0x%x w: %x ws: %s l: %x/r/n", msg.message, &msg.wParam, msg.wParam, msg.lParam);
delete [](TCHAR *)msg.lParam; //注释掉这句你就会看到堆内存地址变化
break;
default:
printf("Unknown msg:0x%x/n",msg.message);
break;
}
Sleep(1);
}
return 0;
}
相关文章推荐
- 用消息在Win32控制台程序多线程间进行通讯
- 计算一段程序进行的时间、 控制台下用Win32 API打开文件对话框
- win32控制台程序使用CfileDialog进行文件读取
- Win32控制台程序,ListBox控件响应鼠标单击及双击消息
- Win32控制台程序是什么
- win32控制台程序里面,使用CString
- HTTP 通讯 消息内容以 XML 的方式进行相互之间的通信 实例
- 用2005创建WIN32控制台程序
- 用win32控制台写C++程序,程序运行完成马上就消失
- 用C语言编写控制台程序进行调试时程序闪退的解决办法
- 在MFC,Win32程序中向控制台(Console)窗口输出调试信息
- VC++/VS2010 Win32控制台程序 运行结束 DOS/dos窗口立刻消失/一闪而过 的解决方法整理
- WndProc 与win32程序消息循环
- 在MFC,Win32程序中向控制台(Console)窗口输出调试信息
- WIN32控制台下的串口通信程序
- 预留位置队列PRQueue:多线程程序中消息输入队列和消息输出队列保持同序
- Win32控制台程序的定时器实现(转载)
- Linux 下监控程序 -- 共享内存通讯,消息订阅发布
- Win32控制台程序中使用STL(string)
- PHP与C(或其它语言)通过消息队列进行通讯,完整代码