CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Jan 2003
    Posts
    104

    Wait for a modeless dialog to finish

    I have a global function which creates a dialog box. I don't want this dialog to block the main window, so I decided to use a modeless dialog box. But the function that displays the dialog box needs to wait until the dialog is closed. The pseudo-code is something like this:

    foo()
    {
    dlg.create();
    dlg.ShowWindow();

    wait until dlg is closed and then continue here
    }

    Does anyone know how to do this? Thank you very much!

  2. #2
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    Have the dialog send a message to your main window when it gets closed. You have to understand that if you add a waiting loop in that function (foo()), this loop will block the main window's message pump, if the function runs in the main thread.

    One option is to switch to multithreading, but that is an overkill. A second option is to call the main window's message pump from inside the waiting loop, but that is an ugly hack.

    In my oppinion, the smoothest solution is the message driven one.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Quote Originally Posted by Gabriel Fleseriu
    You have to understand that if you add a waiting loop in that function (foo()), this loop will block the main window's message pump, if the function runs in the main thread.
    Well...one could use 'MsgWaitForMultipleObjects()' to avoid this, however, I also would suggest to send a 'termination' message to the main dialog...even in regard that this is a modeless dialog box...in other words the user should still be able to work with the main dialog (which would not be the case if the message queue would be blocked)...

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635
    dlg.RunModalLoop();

  5. #5
    Join Date
    Jan 2003
    Posts
    104
    Quote Originally Posted by Gabriel Fleseriu
    One option is to switch to multithreading, but that is an overkill.
    Thank you very much for your hint. It dawns on me that using UI thread would solve the problem nicely.

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Quote Originally Posted by kyoko
    It dawns on me that using UI thread would solve the problem nicely.
    Well...did you read the second part of the sentence by Gabriel...the one with the 'overkill' in it...? From your description, I honestly see no need to add multithreading here...

  7. #7
    Join Date
    Jan 2003
    Posts
    104
    Quote Originally Posted by Andreas Masur
    Well...did you read the second part of the sentence by Gabriel...the one with the 'overkill' in it...? From your description, I honestly see no need to add multithreading here...
    Thank you for your comments. Honestly, I didn't know how to do it without using multithreading. using windows messages alone, doesn't seem to work, because foo() is a global function, and it can't receive windows messages, right?

  8. #8
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    So why does foo need to wait for the modeless dialog to be closed?
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  9. #9
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Well..and to follow souldog....in my eyes the following sentence of you is a contradiction...
    I don't want this dialog to block the main window, so I decided to use a modeless dialog box.
    Why not simply opening the dialog modally...and thus, your application cannot be used by the user as long as the dialog is open. If you need some kind of trigger, simply post a message to the main dialog as soon as the modal dialog is being closed...

  10. #10
    Join Date
    Jan 2003
    Posts
    104
    Quote Originally Posted by Andreas Masur
    Well..and to follow souldog....in my eyes the following sentence of you is a contradiction...

    Why not simply opening the dialog modally...and thus, your application cannot be used by the user as long as the dialog is open. If you need some kind of trigger, simply post a message to the main dialog as soon as the modal dialog is being closed...
    Well, the dialog I want to pop up is a download progress dialog. It it a potentially long lasting process, so I don't want it to block the user to do other operations on the main dialog. On the other way, foo() needs to wait for the dialog to close before it can proceed, because foo() needs to do some operation with the downloaded file, only after the dialog is closed can foo() know the download is complete.

  11. #11
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    So what you need to do is break foo into two funcitons.

    foo1()
    {
    //start download
    //display progress dialog as modeless
    //when done post message to main window
    }

    foo2()
    {
    //this is run when the main window gets the message that download is done
    //do whatever foo did when progress is done.
    }
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  12. #12
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    As souldog pointed out (just like Gabriel), the way to go here is using messages rather than introducing threading...

  13. #13
    Join Date
    Feb 2016
    Posts
    1

    Re: Wait for a modeless dialog to finish

    Quote Originally Posted by GCDEF View Post
    dlg.RunModalLoop();
    This is the easiest, cleanest solution.
    I was looking for similar requirements of a modeless dialog, and this did the trick:
    Code:
    dlg.Create(IDD_MYDialog, CWndOfMainExe);
    dlg.ShowWindow(SW_NORMAL);
    dlg.RunModalLoop(MLF_NOIDLEMSG);

  14. #14
    Join Date
    Feb 2016
    Posts
    16

    Re: Wait for a modeless dialog to finish

    you can run while loop with PeekMessage
    but i doubt it can be done properly
    Last edited by Alexeyn; February 29th, 2016 at 04:59 PM.

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