Skip to content

Commit ddd8077

Browse files
committed
Change debounc extention to use DispatherQueueTimer
1 parent bb5d39d commit ddd8077

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

Microsoft.Toolkit.Uwp.UI.Controls/RangeSelector/RangeSelector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public partial class RangeSelector : Control
6363
/// </summary>
6464
public static readonly DependencyProperty StepFrequencyProperty = DependencyProperty.Register(nameof(StepFrequency), typeof(double), typeof(RangeSelector), new PropertyMetadata(DefaultStepFrequency));
6565

66-
private readonly DispatcherTimer keyDebounceTimer = new DispatcherTimer();
66+
private readonly DispatcherQueueTimer keyDebounceTimer = DispatcherQueue.GetForCurrentThread().CreateTimer();
6767

6868
private Border _outOfRangeContentContainer;
6969
private Rectangle _activeRectangle;

Microsoft.Toolkit.Uwp.UI/Extensions/DispatcherTimerExtensions.cs renamed to Microsoft.Toolkit.Uwp.UI/Extensions/DispatcherQueueTimerExtensions.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44

55
using System;
66
using System.Collections.Concurrent;
7-
using Windows.UI.Xaml;
7+
using Windows.System;
88

99
namespace Microsoft.Toolkit.Uwp.UI.Extensions
1010
{
1111
/// <summary>
12-
/// Set of extention methods for using <see cref="DispatcherTimer"/>.
12+
/// Set of extention methods for using <see cref="DispatcherQueueTimer"/>.
1313
/// </summary>
14-
public static class DispatcherTimerExtensions
14+
public static class DispatcherQueueTimerExtensions
1515
{
16-
private static ConcurrentDictionary<DispatcherTimer, Action> _debounceInstances = new ConcurrentDictionary<DispatcherTimer, Action>();
16+
private static ConcurrentDictionary<DispatcherQueueTimer, Action> _debounceInstances = new ConcurrentDictionary<DispatcherQueueTimer, Action>();
1717

1818
/// <summary>
1919
/// <para>Used to debounce (rate-limit) an event. The action will be postponed and executed after the interval has elapsed. At the end of the interval, the function will be called with the arguments that were passed most recently to the debounced function.</para>
@@ -27,18 +27,18 @@ public static class DispatcherTimerExtensions
2727
/// <param name="immediate">Determines if the action execute on the leading edge instead of trailing edge.</param>
2828
/// <example>
2929
/// <code>
30-
/// private DispatcherTimer _typeTimer = new DispatcherTimer();
30+
/// private DispatcherQueueTimer _typeTimer = new DispatcherQueueTimer();
3131
///
3232
/// _typeTimer.Debounce(async () =>
3333
/// {
3434
/// // Only executes this code after 0.3 seconds have elapsed since last trigger.
3535
/// }, TimeSpan.FromSeconds(0.3));
3636
/// </code>
3737
/// </example>
38-
public static void Debounce(this DispatcherTimer timer, Action action, TimeSpan interval, bool immediate = false)
38+
public static void Debounce(this DispatcherQueueTimer timer, Action action, TimeSpan interval, bool immediate = false)
3939
{
4040
// Check and stop any existing timer
41-
var timeout = timer.IsEnabled;
41+
var timeout = timer.IsRunning;
4242
if (timeout)
4343
{
4444
timer.Stop();
@@ -72,7 +72,7 @@ public static void Debounce(this DispatcherTimer timer, Action action, TimeSpan
7272
private static void Timer_Tick(object sender, object e)
7373
{
7474
// This event is only registered/run if we weren't in immediate mode above
75-
if (sender is DispatcherTimer timer)
75+
if (sender is DispatcherQueueTimer timer)
7676
{
7777
timer.Tick -= Timer_Tick;
7878
timer.Stop();

0 commit comments

Comments
 (0)