Skip to content

Commit

Permalink
Always use current dispatcher to invoke wpf timer (xamarin#7958) fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
krdmllr authored and rmarinho committed Oct 18, 2019
1 parent 3623e99 commit 74d4caa
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<string> _entries = new ObservableCollection<string>();

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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Issue5793.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue6957.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue6130.xaml.cs">
<SubType>Code</SubType>
</Compile>
Expand Down
2 changes: 1 addition & 1 deletion Xamarin.Forms.Platform.WPF/WPFPlatformServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public IIsolatedStorageFile GetUserStoreForApplication()

public void StartTimer(TimeSpan interval, Func<bool> 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) =>
{
Expand Down

0 comments on commit 74d4caa

Please sign in to comment.