An example using threads would be greatly appreciated.
UpdateWindow....I already have a Refresh ...what will UpdateWindow do for me differently?
Main Topics
Browse All TopicsI 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.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
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 (;;) {
DoSomePartOfLengthyProcess
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 DoSomePartOfLengthyProcess
What it does is remove any pending messages in the queue and pass them on for further processing. When all of the messages have been removed the function will return. This allows your app to appear responsive to user and other events such as repainting.
To use code from jkr
void OnSomeEvemt () {
for (;;) {
DoSomePartOfLengthyProcess
DrainMsgQueue ();
}
}
You split the lengthy calculation into smaller parts - DoSomePartOfLengthyProcess
After doing each part you call the function DrainMsgQueue which lets the app proess any pending messages such as repainting (and button presses, keyboard events ....). When it has emptied the queue you go round the loop and perform the next computationally intensive part.
As I mentioned earlier you ought to have some way of stopping/handling the case of the user closing dialog and that is by having a member var to act as some flag that can be set in response to an event such as the WM_CLOSE message
>>You split the lengthy calculation into smaller parts - DoSomePartOfLengthyProcess
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'
Business Accounts
Answer for Membership
by: jaime_olivaresPosted on 2004-08-11 at 15:31:58ID: 11779058
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.