English 中文(简体)
在 MFC 中, 将工作者 Thread 发送到主窗口的信件
原标题:PostMessage from WorkerThread to Main Window in MFC

我有一个 MFC 应用程序, 它有工人线条, 我想做的是将工人线条的信息寄到 GUI 主界面线条上, 以更新 GUI 上的一些状态信息。 到目前为止, 我所做的是 < code> 注册了一个新的窗口信息

//custom messages
static UINT FTP_APP_STATUS_UPDATE = ::RegisterWindowMessageA("FTP_APP_STATUS_UPDATE");

将此信件添加到对话框类的信息映射图

ON_MESSAGE(FTP_APP_STATUS_UPDATE, &CMFC_TestApplicationDlg::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,&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);
    //PostMessage do nothing? what I am doing wrong?
    return 1;
}

问题是当邮递邮件函数被称为 < code> on Status Update 时, 问题在于该函数应该被调用, 但是没有被调用, 没有例外或断言, 我做错了什么? 我已经尝试了 ON_ REGERED_ MESSAGE ON_ MESSAGE 但是没有成功, 没有帮助吗?

最佳回答

CMFC_TestApplicationDlg::OnBnClickedMfcbutton1 () 可能会在线条开始之前返回。这导致您的 ThreadParams 超出范围, 所以当您从线条上访问它时, 您正在访问解开的内存。 您需要以其他方式分配它, 例如 :

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;
}
问题回答

暂无回答




相关问题
Problem statically linking MFC libraries

I have a Visual Studio 6 workspace I m trying to convert to a Visual Studio 2008 solution. The output of said solution is a .dll. It has to be a .dll and it needs to statically link MFC as I can t ...

Switching to WPF. Is it time?

I m considering switching from MFC to WPF. My first concern is that there are too many users who don t have .NET with WPF installed yet. Can anybody point to a source containing the WPF penetration ...

Crash within CString

I am observing a crash within my application and the call stack shows below mfc42u!CString::AllocBeforeWrite+5 mfc42u!CString::operator=+22 No idea why this occuring. This does not occur ...

C# for UI, c++ for library

I have a numerical library coded in C++. I am going to make a UI for the library. I know some MFC. So one solution is to use MFC and make a native application. The alternative is C#. I know nothing ...

Search by using the keyboard in a list/grid - algorithm

I need to implement a custom search in a grid and I would like to find some user interface guidelines that explain the standard way to implement it. I mean this kind of search that is initiated by ...

MFC List Control

In MFC, I can edit the text of items in the list control but only for the first column by setting the Edit Labels to true. Now when I click the first column item to change its text, I m able to change ...

热门标签