Flush Paint messages?

  • Thread starter Thread starter davepkz
  • Start date Start date
D

davepkz

Hello,
I have a simple dialog in C#. When the user presses a button, a long
process occurs. As it goes on, I update some values in the controls on
the dialog.

However, nothing happens until the whole process is done. I tried
calling Invalidate whenver i update the on-screen values, but
presumably it's waiting until my long function is done before it
processes the Paint message.

Is there a way to tell it to flush all paint messages?

thanks
Dave
 
Dave,

The reason this happens is because you are doing all of the work on the
UI thread. When you perform the long running operation, you are blocking
the processing of other windows messages (remember, it runs in a loop to
process these on the UI thread).

There are two solutions to this. The first is to call the static
DoEvents method on the Application class in the System.Windows.Forms
namespace. This method will process the windows messages that are in the
queue, and you should see your repaints.

However, calling this method, IMO, is an abomination. It's dirty.

What you really want to do is spawn a new thread and perform your
processing there. Then, you can update the UI from there. Note that when
you want to update your UI from another thread, you need to call the Invoke
method on a control, passing a delegate with the code to execute on the UI
thread. This is because updates to the UI must come on the thread that the
UI was created on, and this is what Invoke does.

Hope this helps.
 
Nicholas Paldino said:
What you really want to do is spawn a new thread and perform your
processing there. Then, you can update the UI from there. Note that when
you want to update your UI from another thread, you need to call the
Invoke method on a control, passing a delegate with the code to execute on
the UI thread. This is because updates to the UI must come on the thread
that the UI was created on, and this is what Invoke does.

The BackgroundWorker class makes this kind of thing very simple. It does
all the marshalling to the UI thread for you.

http://msdn2.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

-- Alan
 
Hello,
I have a simple dialog in C#. When the user presses a button, a long
process occurs. As it goes on, I update some values in the controls on
the dialog.

However, nothing happens until the whole process is done. I tried
calling Invalidate whenver i update the on-screen values, but
presumably it's waiting until my long function is done before it
processes the Paint message.

Invalidate just queues a refresh to occur when idle. Changing the value
already does this. What you want is probably the Update method which forces
an immediate synchronous repaint.
 
Great, that worked perfectly.

I agree in principle that multithreading is more Architecturally
Correct. And it's a must if the process is time-critical because we
have no guarantee of how long the Paint message will take.

However for simple needs DoEvents appears to be very handy.

Thanks for your help
Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top