Last active
October 20, 2020 16:57
-
-
Save IrisClasson/7552932 to your computer and use it in GitHub Desktop.
Dispatcher.BeginInvoke versus Dispatcher.InvokeAsync
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Data; | |
using System.Windows.Documents; | |
namespace ExampleWPF | |
{ | |
public partial class MainWindow | |
{ | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
CallAsync(); | |
Call(); | |
} | |
//http://msdn.microsoft.com/en-us/library/cc190824(v=vs.110).aspx | |
private void Call() | |
{ | |
Application.Current.Dispatcher.BeginInvoke( | |
//Do stuff | |
); | |
} | |
// .Net 4.5 | |
//http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.invokeasync(v=vs.110).aspx | |
private async void CallAsync() | |
{ | |
await Application.Current.Dispatcher.InvokeAsync( | |
//Dostuff | |
); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void SetCanvas(UIElement image, Point point, bool allowkill) | |
{ | |
Dispatcher.BeginInvoke( | |
new Action( | |
delegate | |
{ | |
Canvas.SetTop(image, point.Y - (finger.ActualHeight/2)); | |
Canvas.SetLeft(image, point.X - (finger.ActualWidth/2)); | |
if (allowkill) | |
TryKillBugs(image, 200, 200); | |
} | |
)); | |
Dispatcher.InvokeAsync(() => | |
{ | |
Canvas.SetTop(image, point.Y - (finger.ActualHeight / 2)); | |
Canvas.SetLeft(image, point.X - (finger.ActualWidth / 2)); | |
if (allowkill) | |
TryKillBugs(image, 200, 200); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment