/// <summary> /// Extension method for CoreDispatcher. Offering an actual awaitable Task with optional result that will be executed on the given dispatcher /// </summary> /// <param name="dispatcher">Dispatcher of a thread to run <paramref name="function"/></param> /// <param name="function"> Function to be executed asynchrounously on the given dispatcher</param> /// <param name="priority">Dispatcher execution priority, default is normal</param> /// <returns>Awaitable Task</returns> public static Task AwaitableRunAsync(this CoreDispatcher dispatcher, Action function, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) => dispatcher.AwaitableRunAsync( () => { function(); return((object)null); }, priority);
public static ContentDialogResult DoThreadedWork(CoreDispatcher dispatcher) { var result = ContentDialogResult.None; var thread = new Thread(DoWork); var waiter = new ManualResetEvent(false); thread.Start(); waiter.WaitOne(); return(result); async void DoWork() { var client = new HttpClient(); var response = await client.GetAsync(new Uri("https://www.microsoft.com")); if (dispatcher.HasThreadAccess) { result = await Handle("Not dispatched."); return; } result = await dispatcher.AwaitableRunAsync(async() => await Handle("Dispatched")); async Task <ContentDialogResult> Handle(string message) { GotHere?.Invoke(message); var dialog = new ContentDialog { Content = response.StatusCode, PrimaryButtonText = "Open", SecondaryButtonText = "Cancel" }; return(await dialog.ShowAsync()); } } }
public Task <bool> EraseBetweenAsync(CoreDispatcher dispatcher, Point pointOne, Point pointTwo) { return(dispatcher.AwaitableRunAsync(() => { Rect result = _container.SelectWithLine(pointOne, pointTwo); if (result.Area() <= 0) { return false; } lock (_strokes) { foreach (InkStroke stroke in _container.GetStrokes().Where(s => s.Selected)) { _strokes.RemoveAll(s => s.Equals(stroke)); } _container.DeleteSelected(); } StrokesChanged?.Invoke(this, EventArgs.Empty); return true; })); }