PostMessage from WorkerThread to Main Window in MFC(从 WorkerThread 到 MFC 中的主窗口的 PostMessage)
问题描述
我有一个 MFC 应用程序,它有一个工作线程,我想做的是从工作线程向主 GUI 线程发布消息,以更新 GUI 上的一些状态消息.到目前为止我所做的是Registered a new window message
I have a MFC application, which has a worker thread, what I want to do is to post message from worker thread to the Main GUI thread to update some status messages on GUI. What I have done so far is Registered a new window message
//custom messages
static UINT FTP_APP_STATUS_UPDATE = ::RegisterWindowMessageA("FTP_APP_STATUS_UPDATE");
将此消息添加到对话框类的消息映射中
Added this message to the message map of dialog class
ON_MESSAGE(FTP_APP_STATUS_UPDATE, &CMFC_TestApplicationDlg::OnStatusUpdate)
OnStatusUpdate
的原型是
afx_msg LRESULT OnStatusUpdate(WPARAM, LPARAM);
定义是
LRESULT CMFC_TestApplicationDlg::OnStatusUpdate(WPARAM wParam, LPARAM lParam)
{
//This function is not called at all.
return 0;
}
而工作线程调用代码是
void CMFC_TestApplicationDlg::OnBnClickedMfcbutton1()
{
ThreadParams params;
params.m_hWnd = m_hWnd;
params.FTPHost = "test_host";
params.FTPUsername = "test";
params.FTPPassword = "test";
AfxBeginThread(FTPConnectThread,¶ms);
}
而工作线程代码是
//child thread function
UINT FTPConnectThread( LPVOID pParam )
{
if(pParam == NULL)
{
return 0;
}
ThreadParams *params = (ThreadParams*)pParam;
OutputDebugString(params->FTPHost);
Sleep(4000); //simulating a network call
CString * message = new CString("Conencted");
PostMessage(params->m_hWnd,FTP_APP_STATUS_UPDATE,0,(LPARAM)message);
//PostMessage do nothing? what I am doing wrong?
return 1;
}
问题是当调用 PostMessage 函数时应该调用 OnStatusUpdate
,但它没有被调用,没有抛出异常或断言,我做错了什么?我试过 ON_REGISTERED_MESSAGE
和 ON_MESSAGE
但没有成功,有什么帮助吗?
the problem is when the PostMessage function is called the OnStatusUpdate
should be called, but it is not being called, no exception or assertion is thrown, What I am doing wrong? I have tried ON_REGISTERED_MESSAGE
and ON_MESSAGE
but no success, any help?
推荐答案
CMFC_TestApplicationDlg::OnBnClickedMfcbutton1()
可能在线程启动前返回.这会导致您的 ThreadParams
超出范围,因此当您从线程访问它时,您正在访问已释放的内存.您需要以其他方式分配它,例如:
CMFC_TestApplicationDlg::OnBnClickedMfcbutton1()
may return before the thread starts. This causes your ThreadParams
to go out of scope, so when you access it from the thread, you are accessing freed memory. You need to allocate it some other way, such as:
void CMFC_TestApplicationDlg::OnBnClickedMfcbutton1()
{
ThreadParams* params = new ThreadParams();
params->m_hWnd = m_hWnd;
params->FTPHost = "test_host";
params->FTPUsername = "test";
params->FTPPassword = "test";
AfxBeginThread(FTPConnectThread,params);
}
//child thread function
UINT FTPConnectThread( LPVOID pParam )
{
if(pParam == NULL)
{
return 0;
}
ThreadParams *params = (ThreadParams*)pParam;
OutputDebugString(params->FTPHost);
Sleep(4000); //simulating a network call
CString * message = new CString("Conencted");
PostMessage(params->m_hWnd,FTP_APP_STATUS_UPDATE,0,(LPARAM)message);
delete params;
return 1;
}
这篇关于从 WorkerThread 到 MFC 中的主窗口的 PostMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 WorkerThread 到 MFC 中的主窗口的 PostMessage
基础教程推荐
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01