From 74d4caa05ac270d430976353d7d2fd96f653f3e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konrad=20M=C3=BCller?= <11095003+krdmllr@users.noreply.github.com> Date: Fri, 18 Oct 2019 16:07:40 +0200 Subject: [PATCH] Always use current dispatcher to invoke wpf timer (#7958) fixes #6957 --- .../Issue6957.cs | 31 +++++++++++++++++++ ...rin.Forms.Controls.Issues.Shared.projitems | 1 + .../WPFPlatformServices.cs | 2 +- 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue6957.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue6957.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue6957.cs new file mode 100644 index 000000000..7274c04e1 --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue6957.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.ObjectModel; +using System.Threading.Tasks; +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; + +namespace Xamarin.Forms.Controls.Issues +{ + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Github, 6957, "Device.StartTimer() won't fire on WPF if it is executed on Non UI thread", PlatformAffected.WPF)] + public class Issue6957 : TestContentPage + { + ObservableCollection _entries = new ObservableCollection(); + + protected override void Init() + { + Device.BeginInvokeOnMainThread(()=> Device.StartTimer(TimeSpan.FromSeconds(2), () => Tick(false))); + Task.Run(() => Device.StartTimer(TimeSpan.FromSeconds(2), () => Tick(true))); + Content = new ListView + { + ItemsSource = _entries + }; + } + + bool Tick(bool fromOtherThread) + { + _entries.Add($"Tick from {(fromOtherThread ? "other thread" : "main thread")}"); + return false; + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index b9ffb0faf..243a39771 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -598,6 +598,7 @@ Code + Code diff --git a/Xamarin.Forms.Platform.WPF/WPFPlatformServices.cs b/Xamarin.Forms.Platform.WPF/WPFPlatformServices.cs index 5047bf14b..3819039cd 100644 --- a/Xamarin.Forms.Platform.WPF/WPFPlatformServices.cs +++ b/Xamarin.Forms.Platform.WPF/WPFPlatformServices.cs @@ -141,7 +141,7 @@ namespace Xamarin.Forms.Platform.WPF public void StartTimer(TimeSpan interval, Func callback) { - var timer = new DispatcherTimer { Interval = interval }; + var timer = new DispatcherTimer(DispatcherPriority.Background, System.Windows.Application.Current.Dispatcher) { Interval = interval }; timer.Start(); timer.Tick += (sender, args) => {