现在的位置: 首页 > 综合 > 正文

线程间通讯

2013年04月24日 ⁄ 综合 ⁄ 共 665字 ⁄ 字号 评论关闭

参考文章连接文章连接:http://www.codeproject.com/cpp/Win32_MQ_MultiThreading.asp

下边的事例程序用到了线程,test继承自CThread,参考下面的链接:

http://blog.csdn.net/wei801004/archive/2006/05/18/744341.aspx

 #define THRD_MESSAGE WM_USER + 2

DWORD WINAPI test::ThreadWorkItem(LPVOID lpParameter)
{
 MSG msg;
 while (true)
 {
  BOOL MsgReturn = ::PeekMessage(&msg, NULL, 0, 0,PM_REMOVE);//程序会立刻返回

 //BOOL MsgReturn = ::GetMessage(&msg, NULL, 0, 0);//程序阻塞接受消息

  if (MsgReturn)
  {
   switch ( msg.message )
   {
   case THRD_MESSAGE:
    {
     break;
    }
   default:
    cout<<"Error!"<<endl;
    break;
   }
  }
 }
 return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
 test oTest;
 oTest.Start();

 PostThreadMessage (oTest.m_dwThreadID, THRD_MESSAGE, 0,0);

 Sleep(100);
}

抱歉!评论已关闭.