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

    Exit application using thread in MFC SDI

    I have a SDI application. I created a method OnClose to handle ON_WM_CLOSE of CMainFrm. This onclose() function calls a method in cmyview.cpp. Here, I created a thread that calls global function and from this function it calls another function in cmyview.cpp. At certain condition my application should close at here, I used postmessgae(WM_CLOSE (or) WM_DESTROY). I am having an error as object reference not set on postmessage(WM_CLOSE) it is going to afxwin2.inl page where exception occurs.

    Below is code snippetHere, either j or k will only be true depneds on user input)

    MainFrm.cpp:
    void CMainFrame::OnClose()
    {
    CMyView* pview = (CMyView*)((CFrameWnd*)AfxGetMainWnd())->GetActiveView();
    pview->method1();
    }

    CMyView.cpp:
    void CMyView::method2()
    {
    int i =1;
    bool j, k
    while(i==1)
    {
    if (j)
    {
    i=2;
    PostMessage(WM_CLOSE);
    }
    else if (j)
    {
    i=2;
    }
    }
    }

    UINT thredproc1(LPVOID lpvoid)
    {
    CMyView* pMyView = (CMyView*)lpvoid;
    pMyView->method2();
    return 0;
    }

    void CMyView::method1()
    {
    CWinThread* pthread = AfxBeginThread(thredproc1,this,0);
    }

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Exit application using thread in MFC SDI

    1. Please use code tags. Your code is unreadable without them.
    2. Does the code you posted actually replicate the problem?
    3. You are using uninitialized variables. j is never assigned a value. Also, your else if will never be executed.
    Code:
        void CMyView::method2()
        {
            int i =1;
            bool j, k
            while(i==1)
            {
                 if (j)
                 {
                    i=2;
                    PostMessage(WM_CLOSE);
                 }
                 else if (j)
                {
                    i=2;
                }
            }
        }
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: Exit application using thread in MFC SDI

    Hello!

    You are only allowed to call MFC user-interface objects from a single thread.

    The reason is that the message loop may pump messages while your thread is running, and objects may be destroyed and modified which would crash your program.

    If you try to do something like your example in C#, it will stop immediately and give you an error. In C++ you will, as you have noticed, instead get an application failure.

    Regards
    Last edited by zerver; February 5th, 2014 at 06:10 AM.
    Nobody cares how it works as long as it works

  4. #4
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: Exit application using thread in MFC SDI

    Also let me add that although you are not allowed to invoke objects from a thread, you can still send a message to the window handle.

    Code:
    CWinThread* pthread = AfxBeginThread(thredproc1,this->m_hWnd,0);
    
    UINT thredproc1(LPVOID lpvoid)
    {
      Sleep(1000);
      ::PostMessage((HWND)lpvoid, WM_CLOSE, 0, 0);
      return 0;
    }
    Last edited by zerver; February 5th, 2014 at 06:24 AM.
    Nobody cares how it works as long as it works

Tags for this Thread

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