CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 1999
    Posts
    15

    Thread and SendMessage

    In the MFC app(Class Doc,View,Mainframe,App) I have my class A. Class A is a member object of the View class.

    classCProcServView: public CListView
    {
    public:
    A m_Process;
    }


    From the View class, I call a function in class A.

    void CProcServView::OnStart()
    {
    m_Process.StartAllProcesses();
    }


    In Class A::StartAllProcesses, I create a thread.

    BOOL A::StartAllProcesses()
    {
    CWinThread *pCreateThread;
    pCreateThread = AfxBeginThread((AFX_THREADPROC) ProcessKillingThread, (LPVOID)this);
    }


    Being the thread, I call SendMessage to MFC app. But to do that, I do the following -

    DWORD ProcessKillingThread(LPVOID lpDummy)
    {
    CProcServApp* pApp = (CProcServApp*)AfxGetApp();
    CWnd* pWnd = (CWnd*)pApp->m_pMainWnd;
    ::SendMessage(pWnd->m_hWnd, WM_UPDATELIST, 0,0);
    }

    But, (CWnd*)pApp->m_pMainWnd shows up as NULL.

    Any idea, how to go from here ?

    -Thanks,





  2. #2
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: Thread and SendMessage

    You should never, and I repeat never, use SendMessage
    from within a thread. Use PostMessage, otherwise you
    might lock up your application.

    When your thread gets started, pass it a HWND and use that
    to POST a message back to the application.

    Sally


  3. #3
    Join Date
    May 1999
    Posts
    15

    Re: Thread and SendMessage

    Thanks for your advice Sally! I figured out the hard way. Its working now.
    You are right, a handle has to be passed to the thread to PostMessage.
    Thanks!





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured