Where is the Application.DoEvents() in WPF? Where is the Application.DoEvents() in WPF? wpf wpf

Where is the Application.DoEvents() in WPF?


Try something like this

public static void DoEvents(){    Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,                                          new Action(delegate { }));}


Well, I just hit a case where I start work on a method that runs on the Dispatcher thread, and it needs to block without blocking the UI Thread. Turns out that msdn explains how to implement a DoEvents() based on the Dispatcher itself:

public void DoEvents(){    DispatcherFrame frame = new DispatcherFrame();    Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,        new DispatcherOperationCallback(ExitFrame), frame);    Dispatcher.PushFrame(frame);}public object ExitFrame(object f){    ((DispatcherFrame)f).Continue = false;    return null;}

(taken from Dispatcher.PushFrame Method)

Some may prefer it in a single method that will enforce the same logic:

public static void DoEvents(){    var frame = new DispatcherFrame();    Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,        new DispatcherOperationCallback(            delegate (object f)            {                ((DispatcherFrame)f).Continue = false;                return null;            }),frame);    Dispatcher.PushFrame(frame);}


The old Application.DoEvents() method has been deprecated in WPF in favor of using a Dispatcher or a Background Worker Thread to do the processing as you have described. See the links for a couple of articles on how to use both objects.

If you absolutely must use Application.DoEvents(), then you could simply import the system.windows.forms.dll into your application and call the method. However, this really isn't recommended, since you're losing all the advantages that WPF provides.