Re: UI Freeze Problem

Tech-Archive recommends: Fix windows errors by optimizing your registry



rahulthathoo wrote:
My problem is that the cpu usage becomes 100% - this is because i need
to refresh the dialog after a sleep of 10 miliseconds. but the problem
is that the sleep makes the UI freeze. I thought a workaround would be
using threads. but even after using threads and events the problem
persists - the UI still freezes. Please tell me what is going wrong
here. Here is the code listing:

Your UI has to be message-driven, not loop-driven. Any "while(TRUE)" loop in the main thread will freeze the UI, and any "while(TRUE)" loop in a secondary thread will cause 100% CPU usage. Eliminate these loops! To keep the UI alive you must (MUST!) structure OnPaint so it paints one time and then returns. (It returns to the message loop to check for any user input.)

You may not be able to get 10 msec repaint rate. But the best you can do would be to set up a timer and call the window's Invalidate in response to each timer message (WM_TIMER). The Invalidate call will generate a new OnPaint after all user input is processed.

If the resulting performance is inadequate for you then you should consider throwing out MFC and studying a game programming book. In MFC you have to work with the MFC message loop; in game programming you write your own message loop and paint when you want to.)

--
Scott McPhillips [VC++ MVP]

.



Relevant Pages

  • Re: Opening website delayed by 30 seconds
    ... Did your process register for DDE (could be implicitly by MFC)? ... web browser, then the call would be essentially equivalent to this: ... between the last call to PeekMessage(the last run of the message loop) ...
    (microsoft.public.win32.programmer.kernel)
  • Re: MSProvide::Logon and Modal Dialog
    ... The benefit of doing it this way is that MFC owns the dialog's ... message loop, whereas the message loop was formerly hidden inside the ... Windows API function::DialogBox. ...
    (microsoft.public.win32.programmer.messaging)
  • Re: UI Freeze Problem
    ... to refresh the dialog after a sleep of 10 miliseconds. ... hEvent = CreateEvent(NULL, ... your UI thread to drop back into its message loop and the worker to ...
    (microsoft.public.vc.mfc)
  • Re: Dialog Accelerators
    ... Accelerator Table for the following Dialog Application using Win32 (no MFC)? ... message loop within the DlgProc, but that is an ugly hack. ... PSTR szCmdLine, int nCmdShow) ...
    (microsoft.public.win32.programmer.ui)
  • Re: CWinThread Cant Receive Custom Message in some case
    ... IIRC, MFC runs its own message loop for modal dialogs, and it must be ... handling thread messages in some way. ...
    (microsoft.public.vc.mfc)