Link to home
Start Free TrialLog in
Avatar of loneill2
loneill2

asked on

Refresh Issue with MFC Dialog

I have an dialog application.  The problem is that the dialog class has functions which take a long time to complete -- thus, causing refresh problems with the dialog.

How do I refresh the dialog (with the OnPaint function) while those functions go off and do their things?  I need to do the refresh within the dialog cpp file and NOT in the functions that take a long time.
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

You have many alternatives, the cleanest in my opinion is to put your heavy processes in different threads, you can use CreateThead() for this. Another alternative is to call UpdateWindow() anywhere inside your processing funtions to make dialog repaint.
Avatar of loneill2
loneill2

ASKER

An example using threads would be greatly appreciated.

UpdateWindow....I already have a Refresh ...what will UpdateWindow do for me differently?
Avatar of jkr
No need to use threads at all - you can just add a call to

void DrainMsgQueue () {

     MSG msg;

     while ( PeekMessage ( &msg, 0, 0, PM_REMOVE))
                DispatchMessage ( &msg);
}

every once in a while, e.g.

void OnSomeEvemt () {

    for (;;) {

        DoSomePartOfLengthyProcessing ();
        DrainMsgQueue ();
    }
}

That will help to keep your dialog up to date and running.
What you are experiencing is the WM_PAINT is of a low priority and your app is busy performing your processing.  Just calling UpdateWindow won't do anything.  You need to have a mechanism to allow any messages in the message queue to be processed.  jkr has given you a very useful piece of code to accomplish that.
I would also suggest in your for loop where you do your processing that you have a flag (bool member var, value modified in the OnClose of your dialog/window) and you only enter your DoSomePartOfLengthyProcessing when the flag has not been set.  This prevents silly things happening if the user is trying to close your dialog and you go back into the computationally intensive function and attempt to access a now non-existant window for example.
I can't find info on DrainMsgQueue..can someone point me to some info or give some more info about it?
>> I can't find info on DrainMsgQueue.

There is no info, that is a 'hand made' function:

void DrainMsgQueue () {

    MSG msg;

    while ( PeekMessage ( &msg, 0, 0, PM_REMOVE))
               DispatchMessage ( &msg);
}

The above is all you need, and it works. Just add it to your program.

SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>You split the lengthy calculation into smaller parts - DoSomePartOfLengthyProcessin

This should not even be necessary, since 'lengthy' processing in almost all 'real life' cases means that a loop of some kind is involved, so this function could just be called on every run of that loop.

BTW, VB has a similar mechanism for that, it is called 'DoEvents'
The Visual C++ compiler is complaining that one parameter is missing from PeekMessage.

Would that be a call to this pointer as a handle to dialog?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial