CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2007
    Posts
    68

    MFC Modeless dialog hangs

    In a 3rd party application I want to invoke an MFC dll which creates a modeless dialog.
    The below is the exported function:

    long CAPLEXPORT far CAPLPASCAL ShowTstDlg(long a, long b, long c)
    {
    TstDlg* pTest = new TstDlg;
    pTest->Create(TstDlg::IDD, NULL);
    pTest->ShowWindow(SW_SHOW);
    return 1;
    }

    in the above code, the modeless dialog freezes.

    So I have modified the code as follows:

    long CAPLEXPORT far CAPLPASCAL ShowTstDlg(long a, long b, long c)
    {
    TstDlg* pTest = new TstDlg;
    pTest->Create(TstDlg::IDD, NULL);
    pTest->ShowWindow(SW_SHOW);


    CString str("MQB RBS Menu");
    HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, str);

    MSG msg;
    while(WaitForSingleObject(hEvent, 0) != WAIT_OBJECT_0)
    {
    while(::GetMessage(&msg, NULL, 0, 0))
    {
    ::TranslateMessage(&msg);
    :ispatchMessage(&msg);
    }
    }

    // event cleanup
    CloseHandle(hEvent);

    return 1;
    }

    In this case, the modeless dialog works fine, but the 3rd part application hangs as the control is never returned to the 3rd party application, due to infinite while loop.


    So I changed as follows:


    long CAPLEXPORT far CAPLPASCAL ShowTstDlg(long a, long b, long c)
    {
    TstDlg* pTest = new TstDlg;
    pTest->Create(TstDlg::IDD, NULL);
    pTest->ShowWindow(SW_SHOW);


    CString str("MQB RBS Menu");
    HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, str);

    MSG msg;
    while(WaitForSingleObject(hEvent, 0) != WAIT_OBJECT_0)
    {
    if(GetActiveWindow() != pTest->GetSafeHwnd()) break
    while(::GetMessage(&msg, NULL, 0, 0))
    {
    if(GetActiveWindow() != pTest->GetSafeHwnd()) break
    ::TranslateMessage(&msg);
    :ispatchMessage(&msg);
    }
    }

    // event cleanup
    CloseHandle(hEvent);

    return 1;
    }

    In this case the menu is active at first. Later when I click on any other application, the again the dialog freezez.

    Can I know how can this be solved?

    Thanks in advance

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: MFC Modeless dialog hangs

    39 posts is enough to have discovered code tags by now.

    If the problem is that your GetMessageLoop never exists, try PeekMessage with the PM_REMOVE flag instead.

    I won't believe WaitForSingleObject should be in a loop either

  3. #3
    Join Date
    Apr 2007
    Posts
    68

    Re: MFC Modeless dialog hangs

    Thanks for the quick reply

    I have even tried the PeekMessage as follows

    MSG msg; // handle dialog messages
    while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
    if(!IsDialogMessage(hwnd,&msg))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    }

    In this case also the Modeless dialog freezes, but the parent is active.

  4. #4
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: MFC Modeless dialog hangs

    I'm presuming that the 3rd party application is executing a long running task when it calls your callback ?

    Also is the 3rd party application a Windows application or a console application ? I'm assuming that it's a console application which is why you're trying to write your own message loop.

    The thing to realise is that windows messages are dispatched on a single thread - so if a long running task is executing on the UI thread then all the UI will hang since the message loop will never be processed.

    One thing to try is to run up a separate thread and then call DoModal() on your dialog in the separate thread. This may work - DoModal() contains its own message loop.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  5. #5
    Join Date
    Apr 2007
    Posts
    68

    Re: MFC Modeless dialog hangs

    Thanks for the reply.

    Actually third party application is a windows application, but it is being developed using KEPL (Not a Microsoft language), which is used for a simulation of embedded programming

    Can you please give a link to create a separate thread to launch modal dialog ( As i am a learner programmer in MFC).

  6. #6
    Join Date
    Apr 2007
    Posts
    68

    Re: MFC Modeless dialog hangs

    I have created a ModalDialog using thread

    void CMenuDlg::OnBnClickedOk()
    {


    HANDLE hCommReadThread = CreateThread(NULL, 0, ThreadFunc, NULL, 0, NULL);
    //hCommReadThread = CreateThread(NULL, 0, ThreadFunc, NULL, 0, NULL);

    if (hCommReadThread != NULL)
    {
    CloseHandle(hCommReadThread);
    hCommReadThread = NULL;
    }

    return;
    }

    DWORD WINAPI ThreadFunc(LPVOID lParam)
    {
    //Ctest oTest; Declared globally
    oTest.DoModal();

    return 1;
    }

    How can I now close the dialog on click of some button?

  7. #7
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: MFC Modeless dialog hangs

    Click of some button
    Is the button in the main ui ? Call another method like the one you've got and call

    Code:
    oTest.Close();
    Close button on the dialog itself should work ok (i.e. cross at top right hand side of the window).

    Darwen.

    P.S. Before I start getting flamed here I'd just like to say that this is not a generally acceptible way of doing this. What you should really be doing is running the long-running task in its own thread and keeping all UI on its own thread. However I am assuming you don't have control over this as it's in a 3rd party application.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

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