Progres windows...

Started by
3 comments, last by JY 18 years, 10 months ago
Greetings! I was doing some coding with MFC Recently and occured following problem. The main application should bring up an window with progress info. The catch is, that not the progress window loads stuff, instead the main application does. Idea: mainform:: begin loading, acquire file, check if exists mainform:: file seems to be ok, bring ob status dialog mainform:: repeat until file read / update status dialog percent status Problem: You can not move m_ProgressWindow() since noone handles it's messages. I can not just call m_ProgressWindow.doModal() because it will wait until terminated. What command should i put in that the m_ProgressWindow will get at least some messages handled? The problem is not only you can not move or drag the window arround, you can not even press cancel if something wen't wrong. I need something like m_ProgressWindow.SetMinMax(0,100); //something like that, i allready have it for (int i=0; i<100; i++) { m_ProgressWindow.UpdatePercentage(); //MoveNext or Increase, that works m_ProgressWindow.HandleMessages(); //What kind of function will do that for me? } Thank you in advance!
Advertisement
Here is how I handle updating progress in tasks: Have an interface class for progress updates, and have your slow tasks take a pointer to one of these for updating the task. Mine looks like this:
class CProgressUpdate{protected:   bool Cancelled;public:   virtual void Update(float Progress) = 0;   bool IsCancelled() { return Cancelled; }};

Have your progress bar window implement that class.
Then have your task processing function take a pointer to that class, and while the task is executing call the Update function when you want to update the progress and check the IsCancelled() function to see if it should stop.
void ExecuteTask(CProgressUpdate * Progress){   int Steps = 100;   for(int i = 0; i < Steps; ++i)   {      if(Progress->IsCancelled())         return;      // do stuff      Progress->Update((float)i / (float)Steps);   }   Progress->Update(1.0f); // if necessary...}

I implemented this progress class 3 times in my project, once for the splash screen's load progress, once for a windows progress bar, and once for an in-game progress bar.

The windows one can be tricky due to the windows messages thing, you'll need to make use of PeekMessage to process any windows messages that might have gotten sent while you are doing your task.

edit: Also for MFC you should check this function out: CWinThread::PumpMessage
Dialog boxes can be modal, or modeless. If you call DoModal you will naturally create a modal dialog. As you correctly state a modal dialog will take control of the application until it has been dismissed, leaving a modeless dialog as the logical alternative.

Call Create on the dialog object instead of DoModal and you will have a modeless dialog box. Then in your loop where you want to update your progress bar, simply call StepIt and call AfxGetApp()->PumpMessage() to force the message to be processed.
"Absorb what is useful, reject what is useless, and add what is specifically your own." - Lee Jun Fan
That does not work!
When I call PumpMessage it waits until I trigger the Progress WIndow with mouse move or other events. If I let the mouse and the computer alone, the progress indicator stands still!

CMyLoader g_Loader;g_Loader.Create(CMyLoader::IDD, this);g_Loader.m_Progress.SetRange(0, 100);g_Loader.m_Progress.SetStep(1);g_Loader.ShowWindow(SW_SHOW);g_Loader.RedrawWindow();Sleep(100);for (int i=0; i&lt;100; i++){	g_Loader.m_Progress.StepIt();	AfxGetApp()-&gt;PumpMessage();	Sleep(5);}g_Loader.DestroyWindow();
Well, StepIt should definitely prompt a message to be put into the queue but maybe not quickly enough for your call to PumpMessage. This is what I usually do, which definitely works - trust me ;)

m_checkProgress->stepProgress();
MSG message;
while (PeekMessage(&message, NULL, 0, 0, 0)) AfxGetApp()->PumpMessage();
"Absorb what is useful, reject what is useless, and add what is specifically your own." - Lee Jun Fan

This topic is closed to new replies.

Advertisement