Thread: Best way to create a Modeless Modal?

  1. #1
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591

    UI modal without being code modal?

    The title is a bit contradictory but what I want to do is probably very common task: I need to display a simple dialog box to inform the user to wait while a process is working in the background. It would just be a window with text to the effect of "please wait, processing instruction....", no buttons or command menu, but my biggest obstacle is finding a way to display a message box that is both modal in that it stops the user from interacting with the main window, but also modeless in that it allows the thread to continue its background processing.
    So far, I have come up with two possible solutions:
    1) Create a modeless dialog box and at the same time disable the main window, re-enabling it and killing the dialog box after processing has completed.
    2) Create a modal dialog box after creating a child thread to do the background processing, and then kill the dialog box after the child thread has completed.

    The first seems a bit hack-ish and the second seems a bit overly complex (but maybe that's just because I haven't done much winapi threading) ... I'm wondering, is there a better way to display a dialog box while preventing user input without blocking the current thread?
    Last edited by @nthony; 01-20-2007 at 03:10 PM. Reason: better title

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Typically, if there is any UI showing, the message queue must be pumped (ie. you can't block for more than a fraction of a second to do processing). When you launch a modal dialog box, the message queue is run inside the dialog function. Usually, if a significant amount of background processing needs to be done, it is done is a different thread so that the UI thread can keep running. Another, more complex, approach is to set a timer and do the processing incrementally in WM_TIMER messages.

    Therefore, I think you will probably need to go with your second option. Creating a thread is not difficult. Variable synchronization can be hard, but since your original thread is just showing a dialog box and not interacting with variables, you shouldn't need any synchronization.

    In the spirit of completeness, here is a link that shows how to implement your first option (UI modal without being code modal). Edit: You've summarised the implementation in your description, so you don't really need the link.

  3. #3
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Thanks again anony! The title of that link has exactly the words I couldn't find to put in the title for this thread. :P
    So now I finally have an excuse to sit down and fully read the documentation on winapi threading... but before I get the pot of coffee on, I'm wondering, where does native C threading play into all of this? Does winapi threading just use wrappers for C threading functions? Could I say, use pthread_create() instead of CreateThread() with the same effects?

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    pthread_create is a posix function. Posix is implemented by nix and NixOnWin32 (Cygwin for example) platforms, but not by normal Windows compilers (such as MSVC and Dev-C++).

    It is usually a good idea to use _beginthreadex to create threads if using the C or C++ standard libraries.

    _beginthreadex documentation

    Samples

  5. #5
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Those are some very useful links, thanks. About the necessary libraries: I am having problems finding the libcmt library (I'm using CodeBlocks), I've found the msvcrt library (libmsvcrt.a), and tried using an imported libcmt from Visual Studio, but the compiler complained about that. What library should I be looking to include as "libcmt" when using CodeBlocks? (and Dev-C++ too because I think they use the same library names)
    Also, why does __beginthreadex use the libcmt and msvcrt libraries, but not CreateThread?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't create child windows
    By OnionKnight in forum Windows Programming
    Replies: 4
    Last Post: 04-10-2011, 04:13 PM
  2. Create a file from c++ program
    By Dan17 in forum C++ Programming
    Replies: 2
    Last Post: 05-08-2006, 04:25 PM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  4. How to create a file association program?
    By eShain in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2006, 12:15 PM
  5. How to Create reference to an array in C++
    By shiv_tech_quest in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2002, 10:01 AM