Get the dispatcher before the Task.

This commit is contained in:
Tracy Pearson
2022-08-12 12:30:18 -04:00
parent 9bd1952d4c
commit ff9c9c7b55

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Threading; using System.Windows.Threading;
@@ -23,22 +24,22 @@ namespace WpfViewModelFirst
{ {
CurrentViewModel = null; CurrentViewModel = null;
CurrentViewModel = new Part1ViewModel(Part1Action); CurrentViewModel = new Part1ViewModel(Part1Action);
// MainFrameViewModel = new Part2ViewModel(); // MainFrameViewModel = new Part2ViewModel();
} }
private void Part1Action() private void Part1Action()
{ {
var currentDispatcher = Dispatcher.CurrentDispatcher;
CurrentViewModel = null; CurrentViewModel = null;
Task delay = Task.Run(LongDelay).ContinueWith(c => Task delay = Task<string>.Run(LongDelay).ContinueWith(c =>
{ {
System.Diagnostics.Debug.WriteLine(c.Result); System.Diagnostics.Debug.WriteLine(c.Result);
CurrentViewModel = new Part2ViewModel(); CurrentViewModel = new Part2ViewModel();
Strings.Add($"{DateTime.Now}"); currentDispatcher.Invoke(() =>
{
Strings.Add($"{DateTime.Now}");
}
);
}, TaskContinuationOptions.OnlyOnRanToCompletion); }, TaskContinuationOptions.OnlyOnRanToCompletion);
//Task.Run(async () => await LongDelay()).ContinueWith(c =>
// {
// System.Diagnostics.Debug.WriteLine(c.Result);
//CurrentViewModel = new Part2ViewModel();
//}, TaskContinuationOptions.OnlyOnRanToCompletion);
} }
private async Task<string> LongDelay() private async Task<string> LongDelay()