Processing System Messages

Environment: VC6

The Requirement

I needed to run a loop in a modal dialog box, but found that although the UpdateWindow() function updated the window (and did not leave me with a White Box on the screen), the buttons onscreen were unresponsive. I searched the Internet and found articles on how to open a dialog in another thread and to keep monitoring the secondary dialog, but was not happy with the solution.

The Solution

I finally came up with a solution to process the dialog box's messages in a function within the dialog box itself. It works great.

My solution consists of adding a function to the existing dialog class and calling it to process the messages.

  BOOL CMyDialog::ProcessDialogMessages()
  {
    Sleep(0);                                  // Yield to other
                                               // system processes

    // Process the message queue
    MSG dlgMsg;
    if (::PeekMessage (&dlgMsg, NULL, 0, 0, PM_NOREMOVE))
                                               // If there are
                                               // pending messages
    {
       if (!AfxGetApp ()->PumpMessage ()) {    // Allow them to be
                                               // processed
          ::PostQuitMessage (0);               // When no more
                                               // messages exist,
                                               // return WM_QUIT
                                               // should return 0
          return TRUE;                         // Return the caller,
                                               // indicating that
                                               // we just processed
                                               // a Message and that
                                               // there may be more
    }
  }

   // Simulate idle processing.
   LONG lIdle = 0;
   while (AfxGetApp ()->OnIdle (lIdle++));
     return FALSE;                             // Return to Caller,
                                               // saying that we
                                               // did not process
                                               // any messages
  }

Downloads

Download demo project - 4 Kb
Download source - 26 Kb


Comments

  • Fibers

    Posted by Legacy on 07/23/2003 12:00am

    Originally posted by: John

    If you don't want to use threads in your program, you could also try using fibers. Fibers can be scheduled within one thread according to your own algorithm, suiting a program like this quite well. See Jeffrey Richter's "Programming Applications for Microsoft Winodws" (5th Ed.) or MSDN for more information.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Savvy enterprises are discovering that the cloud holds the power to transform IT processes and support business objectives. IT departments can use the cloud to redefine the continuum of development and operations—a process that is becoming known as DevOps. Download the Executive Brief DevOps: Why IT Operations Managers Should Care About the Cloud—prepared by Frost & Sullivan and sponsored by IBM—to learn how IBM SmartCloud Application services provide a robust platform that streamlines …

  • Live Event Date: August 14, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Data protection has long been considered "overhead" by many organizations in the past, many chalking it up to an insurance policy or an extended warranty you may never use. The realities of today make data protection a must-have, as we live in a data driven society. The digital assets we create, share, and collaborate with others on must be managed and protected for many purposes. Check out this upcoming eSeminar and join eVault Chief Technology …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds