Skip to content

Instantly share code, notes, and snippets.

@IrisClasson
Last active October 20, 2020 16:57
Show Gist options
  • Save IrisClasson/7552932 to your computer and use it in GitHub Desktop.
Save IrisClasson/7552932 to your computer and use it in GitHub Desktop.
Dispatcher.BeginInvoke versus Dispatcher.InvokeAsync
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
);
}
}
}
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