WPF: refresh a window during processing

  • Thread starter Thread starter Jeremy
  • Start date Start date
J

Jeremy

In other languages I've worked with, there is a way to allow an app to go
idle just long enough for its windows to refresh. Can't find this in c#. I
did find some discussion of multithreading the app, but that seems like a
bunch of work to retrofit, given that I'd have to learn how to do it. Am I
missing something?

Jeremy
 
Jeremy,

The best approach here is to use multithreading, as you have seen in
other threads. You will want to perform your work on another thread, and
then update the UI thread from the worker thread. Using something like
DoEvents (which is for windows forms, not for WPF, I am not sure if WPF has
an analogy) is just bad practice, and can introduce race conditions that are
unexpected if not accounted for.

In WPF, you would use the Dispatcher class to send notifications to the
UI thread.
 
In other languages I've worked with, there is a way to allow an app to go
idle just long enough for its windows to refresh. Can't find this in c#.
I did find some discussion of multithreading the app, but that seems like
a bunch of work to retrofit, given that I'd have to learn how to do it.
Am I missing something?

Try this:

void DoEvents()
{
DispatcherFrame f = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
(SendOrPostCallback)delegate(object arg)
{
DispatcherFrame fr = arg as DispatcherFrame;
fr.Continue = false;
}, f);
Dispatcher.PushFrame(f);
}

But beware that this can give you problems with re-entrancy (just as VB6's
DoEvents could).

Chris Jobson
 
Back
Top