Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the Application.DoEvents() in WPF?

Tags:

c#

.net

wpf

xaml

I have the following sample code that zooms each time a button is pressed:

XAML:

<Window x:Class="WpfApplication12.MainWindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         Title="MainWindow" Height="350" Width="525">      <Canvas x:Name="myCanvas">          <Canvas.LayoutTransform>             <ScaleTransform x:Name="myScaleTransform" />         </Canvas.LayoutTransform>           <Button Content="Button"                  Name="myButton"                  Canvas.Left="50"                  Canvas.Top="50"                  Click="myButton_Click" />     </Canvas> </Window> 

*.cs

public partial class MainWindow : Window {     public MainWindow()     {         InitializeComponent();     }      private void myButton_Click(object sender, RoutedEventArgs e)     {         Console.WriteLine("scale {0}, location: {1}",              myScaleTransform.ScaleX,             myCanvas.PointToScreen(GetMyByttonLocation()));          myScaleTransform.ScaleX =             myScaleTransform.ScaleY =             myScaleTransform.ScaleX + 1;                  Console.WriteLine("scale {0}, location: {1}",             myScaleTransform.ScaleX,             myCanvas.PointToScreen(GetMyByttonLocation()));     }          private Point GetMyByttonLocation()     {         return new Point(             Canvas.GetLeft(myButton),             Canvas.GetTop(myButton));     } } 

the output is:

scale 1, location: 296;315 scale 2, location: 296;315  scale 2, location: 346;365 scale 3, location: 346;365  scale 3, location: 396;415 scale 4, location: 396;415 

as you can see, there is a problem, that I thought solve by using Application.DoEvents(); but... it does not exist a priori in .NET 4.

What to do?

like image 888
serhio Avatar asked Dec 21 '10 17:12

serhio


People also ask

What does Application DoEvents () do?

Typically, you use this method in a loop to process messages. Calling this method causes the current thread to be suspended while all waiting window messages are processed. If a message causes an event to be triggered, then other areas of your application code may execute.

What is application DoEvents in C#?

DoEvents() can be used to process the messages waiting in the queue on the UI thread when performing a long-running task in the UI thread. This has the benefit of making the UI seem more responsive and not "locked up" while a long task is running.

What is application DoEvents in VB net?

Description. Allows the operating system to process events and messages waiting in the message queue. For example, you can allow a user to click a Cancel button while a processor- intensive operation is executing. In this case, without DoEvents, the click event is not processed until after the operation had completed.


2 Answers

Try something like this

public static void DoEvents() {     Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,                                           new Action(delegate { })); } 
like image 117
Fredrik Hedblad Avatar answered Oct 06 '22 08:10

Fredrik Hedblad


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); } 
like image 25
flq Avatar answered Oct 06 '22 09:10

flq