简体   繁体   English

MFC C ++从工作线程向主ui线程发送带有后消息的自定义用户消息

[英]mfc c++ send a custom user message with postmessage from a working thread to main ui thread

Using Visual Studio 2015 (Community Edition) & MFC C++ project. 使用Visual Studio 2015(社区版)和MFC C ++项目。 I have a worker thread where I wish to use PostMessage() function to send data from this thread to the main UI thread (where my CDialog resides) yet in the same class, that I want to receive this message. 我有一个工作线程,我希望使用PostMessage()函数将数据从该线程发送到主UI线程(我的CDialog所在的位置),但仍在同一类中,我想接收此消息。

In the MyComm.h file, I have the following: 在MyComm.h文件中,我具有以下内容:

#define WM_USERRESPONSE WM_APP + 2000

class MyComm: public CDialog
{
  ...
  CWnd* m_pParent;
  static BOOL m_bThreadKill;
  static CWinThread* pThread;
  static CEvent* pEvent;
  static CEvent m_ThreadKillEvent;
  ...
  static UINT MyThreadProc(LPVOID pParam);
  ...
  afx_msg LRESULT OnResponse(WPARAM wParam, LPARAM lParam);
  ...
};

In my MyComm.cpp file, I have the following: 在我的MyComm.cpp文件中,我具有以下内容:

MyComm::MyComm(CWnd* pParent /*=NULL*/)
    : CDialog(IDD_PPAGE_COMMAND, pParent)
{
  m_pParent = pParent;

  pEvent = new CEvent(FALSE, FALSE);

  if ((pThread = AfxBeginThread(MyThreadProc, this)) == NULL)
    AfxMessageBox("Could not Create Read Thread!");

  pThread->m_bAutoDelete = FALSE;
  m_ThreadKillEvent.ResetEvent();
  m_ReadThreadDead.ResetEvent();
  running = 1;
}

UINT MyComm::MyThreadProc(LPVOID pParam)
{
  MyComm *pMyHndl = ((MyComm*)pParam);
  string s = "I would like this string posted";
  BOOL b = false;

  b = ::PostMessage(pMyHndl->GetSafeHwnd(), WM_USBRDRESPONSE, 0, 
       (LPARAM)&s);
}

BEGIN_MESSAGE_MAP(MyComm, CDialog)
    ON_MESSAGE(WM_USERRESPONSE, &MyComm::OnResponse)
END_MESSAGE_MAP()

afx_msg LRESULT MyComm::OnResponse(WPARAM wParam, LPARAM lParam)
{
  MyStruct* p = (MyStruct*)lParam;
  ...
}

Note I abbreviated some of this to stay on topic. 注意,我将其中的一些缩写保留在主题上。

In debugging this (there is much more code than this), I verify that the thread starts, I execute this PostMessage() function which returns true. 在调试此代码(比这更多的代码)时,我验证线程已启动,并执行此PostMessage()函数,该函数返回true。 I never get to the OnResponse() function that is intended to be the recipient. 我从来没有去过打算作为接收者的OnResponse()函数。 I am not sure why..(??).. 我不确定为什么..(??)..

Some thoughts. 一些想法。 It is true that the class MyComm is in the the same class yet is not the dialog thread, yet is spawned by it and is derived from CDialog. 的确,类MyComm在同一个类中,但不是对话框线程,而是由它派生的,并且是从CDialog派生的。 This may not be sufficient?? 这可能还不够?? I admit, I am still somewhat new to threads via MFC programming paradigm. 我承认,通过MFC编程范例,我对线程还是有些陌生。 Any help is appreciated. 任何帮助表示赞赏。

Maddog 疯狗

  • The variable string s will not exist after thread finishes. 线程完成后,变量string s将不存在。 The thread function you've shown is too small, and would exit soon. 您显示的线程函数太小,将很快退出。
  • You should ideally start new thread from OnInitDialog not in the constructor. 理想情况下,您应该从OnInitDialog而不是在构造函数中启动新线程。 The message loop is not created in constructor, but in while in OnInitDialog . 消息循环不是在构造函数中创建的,而是在OnInitDialog
  • Check that message code is correct. 检查消息代码是否正确。 Your code has both: WM_USBRDRESPONSE and WM_USBRDRESPONSE 您的代码同时具有: WM_USBRDRESPONSEWM_USBRDRESPONSE

Today, I did find the answer to this problem. 今天,我确实找到了这个问题的答案。

It is because the class where the thread is started is not a class tied to a window yet, is spawned by one with a window. 这是因为启动线程的类还不是与窗口绑定的类,而是由带有窗口的类生成的。 So a subtle nuance on pMyHndl is necessary. 因此,对pMyHndl的细微差别是必要的。 If in the POSTMESSAGE() I instead use pMyHndl->m_pParent in place of where m_pParent points to the Parent class (one with window). 如果在POSTMESSAGE()我改为使用pMyHndl->m_pParent代替m_pParent指向Parent类的位置(一个带窗口的位置)。 This code was receiving the message now that the code to receive the message was moved there. 现在,此代码正在接收消息,因为接收消息的代码已移动到那里。 Thanks for all the help everyone. 谢谢各位的帮助。

maddog 疯狗

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM