Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7979897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:54:12+00:00 2026-06-04T09:54:12+00:00

I have a MFC application, which has a worker thread, what I want to

  • 0

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)

The prototype of OnStatusUpdate is

afx_msg LRESULT OnStatusUpdate(WPARAM, LPARAM);

and definition is

LRESULT CMFC_TestApplicationDlg::OnStatusUpdate(WPARAM wParam, LPARAM lParam)
{

     //This function is not called at all.
     return 0;
}

and the worker thread calling code is

void CMFC_TestApplicationDlg::OnBnClickedMfcbutton1()
{
    ThreadParams params;
    params.m_hWnd = m_hWnd;
    params.FTPHost = "test_host";
    params.FTPUsername = "test";
    params.FTPPassword = "test";

    AfxBeginThread(FTPConnectThread,&params);
}

and Worker thread code is

//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;
}

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T09:54:13+00:00Added an answer on June 4, 2026 at 9:54 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have C++ application which has UI developed using MFC, does some networking using
I have a win32 GUI (MFC) application which I need to port to a
I have an MFC application, which i don't want to be closed during the
I have an MFC application. It has a CMap which contains certain data objects,
I have a simple MFC MDI application, where there is main CFormView, in which
I have an application which downloads some data and I want to show that
I have an MFC application to which I want to incorporate multi-threading. Originally I
I have an MFC App which fires up a separate thread for downloading some
I have successfully upgraded an MFC application which was compiled with an old version
I have a MFC dialog based application in which I use ::system() function to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.