线程间如何进行消息传递

来源:互联网 发布:jmeter post请求 json 编辑:程序博客网 时间:2024/06/06 12:26
线程间如何进行消息传递
1)定义消息ID
#define WM_USER_MSG WM_USER+N

2)创建线程
m_hThread = CreateThread(NULL, 0, ThreadProc, 0, 0, &m_dwID);

3)线程函数中使用GetMessage
DWORD WINAPI Thread(LPVOID lpParameter)
{
while (1)
{
MSG msg;  
GetMessage(&msg,0,0,0);  
switch(msg.message)  
{  
case WM_USER_MSG:  
TRACE("接收到消息\n");
break;  
default:  
break;  
}
}
}


4) 在其它线程中发送消息
PostThreadMessage(m_dwID, WM_USER_MSG, 0, 0);