How can I update my win32 GUI application while it waits for another program to finish?

I am currently using CreateProcess / WaitForSingleObject from a win32 GUI application to launch a small graphical application that deals with software licensing issues. All this works fine, but the “parent” application basically hangs while it expects the licensing application to complete its work. During this time, the parent application does not update, and it ends with ugly white squares if the utility application window moves.

Also, for some strange reason, while the utility application is working, if I copy something from this application to the clipboard, it is HANGS. I still do not understand why, but this only happens if I wait for the application to end from the parent application.

So, I think that if I can get the parent application to process its events while waiting for the completion of my other application, this can solve both problems.

So, is there a replacement for CreateProcess / WaitForSingleObject that also handles UI updates?

+3
source share
5 answers

, , WaitForSingleObject() , , .

, , , . , , , .

WaitForSingleObject() MsgWaitForMultipleObjects(). QS_ALLINPUT dwWaitMask, MsgWaitForMultipleObjects , , . MsgWaitForMultipleObjects() , , :

MSG msg;
DWORD reason = WAIT_TIMEOUT;
while (WAIT_OBJECT_0 != reason) {
    reason = MsgWaitForMultipleObjects(1, &hChildProcess, FALSE, INFINITE, QS_ALLINPUT);
    switch (reason) {
    case WAIT_OBJECT_0:
        // Your child process is finished.
        break;
    case (WAIT_OBJECT_0 + 1):
        // A message is available in the message queue.
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
            // Note that if your main message loop does additional processing
            // (such as calling IsDialogMessage() for modeless dialogs)
            // you will want to do those things here, too.
        }
        break;
    }
}
+5

WaitForSingleObject dwMilliseconds.

- WaitForSingleObject WAIT_OBJECT_0.

, , - , . , , .

// Assuming hYourWaitHandle is the handle that you're waiting on
//   and hwnd is your window handle, msg is a MSG variable and
//   result is a DWORD variable
//

// Give the process 33 ms (you can use a different value here depending on 
//  how responsive you wish your app to be)
while((result = WaitForSingleObject(hYourWaitHAndle, 33)) == WAIT_TIMEOUT)
{ 
   // if after 33 ms the object handle is not signaled..       

   // we examine the message queue and if ther eare any waiting..
   //  Note:  see PeekMessage documentation for details on how to limit
   //         the types of messages to look for
   while(PeekMessage(&msg, hwnd,  0, 0, PM_NOREMOVE))
   {
     // we process them..
     if(GetMessage(&msg, NULL, 0, 0) > 0)
     {
       TranslateMessage(&msg);
       DispatchMessage(&msg);
     }
   }
} 
// if you're here it means WaitForSingleObject returned WAIT_OBJECT_0, so you're done
//  (but you should always check and make sure it really is WAIT_OBJECT_0)
if(result != WAIT_OBJECT_0)
{
    // This should not be.. so react!
}

>

+3

:

  • CreateProcess, ,
  • , Window (, WM_PAINT)
  • , (, API PostMessage RegisterWindowMessage).
  • , PostMessage
  • Windows (WM_TIMER), , , .
+1

, , , sendmessage, . :

- , , . SendMessage (HWND_BROADCAST HWND_TOPMOST), SendMessage , , - , t .... , .... DEADLOCK.

, (), . :

  • PeekMessage (yuck)
  • API- MsgWaitForMultipleObjects.

... , , IPC, , ChrisW.

0

, :

  • CreateProcess()

  • WaitForSingleObject(), - ,

  • CloseHandle()

  • PostMessage()

, , , , , , , . , , , . , , , .

0

Source: https://habr.com/ru/post/1703181/


All Articles