Mateusz
Mateusz

Reputation: 109

my PROGRESS BAR redraws not all, WINAPI

in my application I am entering a loop of unknown number of elements, so I am showing a progress bar to inform that application is running,

and this code works FINE, progres bar is refreshing independly:

do
{                      
   ...
   SendMessage(hPBLoading, PBM_STEPIT, 0, 0);       
   ...
} while(true);

but unfortunately the rest of window doesn't refresh (which is obvious due to the loop) and in Windows 7 after a few seconds Windows treat my app like it break down, and it back and refresh after ending the loop,

so I figured out that I need to dispatch the message queue and I changed my code to this:

do
{                      
   ...
   SendMessage(hPBLoading, PBM_STEPIT, 0, 0);       
   ...
   us_tmpBreakCounter++;
   ...

   if (us_tmpBreakCounter >= 10)
   {    
      us_tmpBreakCounter = 0;    
      UpdateSomeOtherElementsinWindow();

    if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) == TRUE) 
      {
       TranslateMessage(&msg);
       DispatchMessage(&msg);   
      }
   }
} while(true);

It works fine, the applcation is updated and refreshed after ex. 10 counts so its never break down, but unfortunately another problem occured, now the PROGRESS BAR doesn't redraw fully - the beveled frame dissapears after a few seconds, check this image below:

http://mstanisz.website.pl/temp/progressbar_01.jpg

Thanks For Your Help! m.

Upvotes: 0

Views: 480

Answers (1)

Eran
Eran

Reputation: 22030

Seems like you're using a GUI thread to do some long task. That's not a good practice. When you want to perform long tasks, use a worker thread for that, and let the GUI thread handle the GUI while the worker works. You'll have to do some extra work taking care of synchronization between the two threads (stop the worker if the GUI is closed, avoid starting a task before the current one has ended and so on), but that's the way it works.

Upvotes: 3

Related Questions