mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 03:07:04 +02:00
Replace DelayInvoke with Task + Async
This commit is contained in:
@@ -1,54 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace Wox
|
||||
{
|
||||
public static class DispatcherExtensions
|
||||
{
|
||||
private static Dictionary<string, DispatcherTimer> timers =
|
||||
new Dictionary<string, DispatcherTimer>();
|
||||
private static readonly object syncRoot = new object();
|
||||
|
||||
public static void DelayInvoke(this Dispatcher dispatcher, string namedInvocation,
|
||||
Action action, TimeSpan delay,
|
||||
DispatcherPriority priority = DispatcherPriority.Normal)
|
||||
{
|
||||
lock (syncRoot)
|
||||
{
|
||||
if (string.IsNullOrEmpty(namedInvocation))
|
||||
{
|
||||
namedInvocation = Guid.NewGuid().ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
RemoveTimer(namedInvocation);
|
||||
}
|
||||
var timer = new DispatcherTimer(delay, priority, (s, e) =>
|
||||
{
|
||||
RemoveTimer(namedInvocation);
|
||||
action();
|
||||
}, dispatcher);
|
||||
timer.Start();
|
||||
timers.Add(namedInvocation, timer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void CancelNamedInvocation(this Dispatcher dispatcher, string namedInvocation)
|
||||
{
|
||||
lock (syncRoot)
|
||||
{
|
||||
RemoveTimer(namedInvocation);
|
||||
}
|
||||
}
|
||||
|
||||
private static void RemoveTimer(string namedInvocation)
|
||||
{
|
||||
if (!timers.ContainsKey(namedInvocation)) return;
|
||||
timers[namedInvocation].Stop();
|
||||
timers.Remove(namedInvocation);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user