From 1a8efdbf2c7eb6b208088f429df7621cc75f1012 Mon Sep 17 00:00:00 2001 From: bao-qian Date: Wed, 6 Jan 2016 06:31:17 +0000 Subject: [PATCH] Replace DelayInvoke with Task + Async --- Wox/Helper/DispatcherExtensions.cs | 54 ------------- Wox/HotkeyControl.xaml.cs | 22 ++--- Wox/MainWindow.xaml.cs | 7 +- Wox/Msg.xaml.cs | 125 ++++++++++++++++------------- Wox/ResultPanel.xaml.cs | 8 +- Wox/Wox.csproj | 9 --- 6 files changed, 88 insertions(+), 137 deletions(-) delete mode 100644 Wox/Helper/DispatcherExtensions.cs diff --git a/Wox/Helper/DispatcherExtensions.cs b/Wox/Helper/DispatcherExtensions.cs deleted file mode 100644 index 72880e2ebf..0000000000 --- a/Wox/Helper/DispatcherExtensions.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Windows.Threading; - -namespace Wox -{ - public static class DispatcherExtensions - { - private static Dictionary timers = - new Dictionary(); - 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); - } - - } -} diff --git a/Wox/HotkeyControl.xaml.cs b/Wox/HotkeyControl.xaml.cs index 783e3e59da..ae38b9f904 100644 --- a/Wox/HotkeyControl.xaml.cs +++ b/Wox/HotkeyControl.xaml.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Input; @@ -39,10 +40,10 @@ namespace Wox SpecialKeyState specialKeyState = GlobalHotkey.Instance.CheckModifiers(); var hotkeyModel = new HotkeyModel( - specialKeyState.AltPressed, - specialKeyState.ShiftPressed, - specialKeyState.WinPressed, - specialKeyState.CtrlPressed, + specialKeyState.AltPressed, + specialKeyState.ShiftPressed, + specialKeyState.WinPressed, + specialKeyState.CtrlPressed, key); var hotkeyString = hotkeyModel.ToString(); @@ -52,12 +53,11 @@ namespace Wox return; } - Dispatcher.DelayInvoke("HotkeyAvailabilityTest", - () => - { - SetHotkey(hotkeyModel); - }, - TimeSpan.FromMilliseconds(500)); + Dispatcher.InvokeAsync(async () => + { + await Task.Delay(500); + SetHotkey(hotkeyModel); + }); } public void SetHotkey(HotkeyModel keyModel, bool triggerValidate = true) @@ -94,7 +94,7 @@ namespace Wox { try { - HotkeyManager.Current.AddOrReplace("HotkeyAvailabilityTest", CurrentHotkey.CharKey, CurrentHotkey.ModifierKeys, (sender, e) => {}); + HotkeyManager.Current.AddOrReplace("HotkeyAvailabilityTest", CurrentHotkey.CharKey, CurrentHotkey.ModifierKeys, (sender, e) => { }); return true; } diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index 804e9cceac..466ef5d705 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Net; using System.Reflection; using System.Threading; +using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Input; @@ -484,13 +485,14 @@ namespace Wox } } _lastQuery = query; - Dispatcher.DelayInvoke("ShowProgressbar", () => + Dispatcher.InvokeAsync(async () => { + await Task.Delay(150); if (!string.IsNullOrEmpty(query.RawQuery) && query.RawQuery == _lastQuery.RawQuery && !_queryHasReturn) { StartProgress(); } - }, TimeSpan.FromMilliseconds(150)); + }); PluginManager.QueryForAllPlugins(query); } StopProgress(); @@ -829,6 +831,7 @@ namespace Wox private void UpdateResultView(List list, PluginMetadata metadata, Query originQuery) { + Thread.Sleep(3000); _queryHasReturn = true; progressBar.Dispatcher.Invoke(new Action(StopProgress)); diff --git a/Wox/Msg.xaml.cs b/Wox/Msg.xaml.cs index 491fa7cb89..e687397036 100644 --- a/Wox/Msg.xaml.cs +++ b/Wox/Msg.xaml.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Threading.Tasks; using System.Windows; using System.Windows.Forms; using System.Windows.Input; @@ -7,71 +8,81 @@ using System.Windows.Media.Animation; using System.Windows.Media.Imaging; using Wox.Helper; -namespace Wox { - public partial class Msg : Window { - Storyboard fadeOutStoryboard = new Storyboard(); - private bool closing = false; +namespace Wox +{ + public partial class Msg : Window + { + Storyboard fadeOutStoryboard = new Storyboard(); + private bool closing = false; - public Msg() { - InitializeComponent(); - var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position); - var dipWorkingArea = WindowIntelopHelper.TransformPixelsToDIP(this, + public Msg() + { + InitializeComponent(); + var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position); + var dipWorkingArea = WindowIntelopHelper.TransformPixelsToDIP(this, screen.WorkingArea.Width, - screen.WorkingArea.Height); - Left = dipWorkingArea.X - this.Width; - Top = dipWorkingArea.Y; - showAnimation.From = dipWorkingArea.Y; - showAnimation.To = dipWorkingArea.Y - Height; + screen.WorkingArea.Height); + Left = dipWorkingArea.X - this.Width; + Top = dipWorkingArea.Y; + showAnimation.From = dipWorkingArea.Y; + showAnimation.To = dipWorkingArea.Y - Height; - // Create the fade out storyboard - fadeOutStoryboard.Completed += new EventHandler(fadeOutStoryboard_Completed); - DoubleAnimation fadeOutAnimation = new DoubleAnimation(dipWorkingArea.Y - Height, dipWorkingArea.Y, new Duration(TimeSpan.FromSeconds(0.3))) { - AccelerationRatio = 0.2 - }; - Storyboard.SetTarget(fadeOutAnimation, this); - Storyboard.SetTargetProperty(fadeOutAnimation, new PropertyPath(TopProperty)); - fadeOutStoryboard.Children.Add(fadeOutAnimation); + // Create the fade out storyboard + fadeOutStoryboard.Completed += new EventHandler(fadeOutStoryboard_Completed); + DoubleAnimation fadeOutAnimation = new DoubleAnimation(dipWorkingArea.Y - Height, dipWorkingArea.Y, new Duration(TimeSpan.FromSeconds(0.3))) + { + AccelerationRatio = 0.2 + }; + Storyboard.SetTarget(fadeOutAnimation, this); + Storyboard.SetTargetProperty(fadeOutAnimation, new PropertyPath(TopProperty)); + fadeOutStoryboard.Children.Add(fadeOutAnimation); - imgClose.Source = new BitmapImage(new Uri("Images\\close.pn", UriKind.Relative)); - //imgClose.Source = new BitmapImage(new Uri(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "Images\\close.png"))); - imgClose.MouseUp += imgClose_MouseUp; - } + imgClose.Source = new BitmapImage(new Uri("Images\\close.pn", UriKind.Relative)); + //imgClose.Source = new BitmapImage(new Uri(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "Images\\close.png"))); + imgClose.MouseUp += imgClose_MouseUp; + } - void imgClose_MouseUp(object sender, MouseButtonEventArgs e) { - if (!closing) { - closing = true; - fadeOutStoryboard.Begin(); - } - } + void imgClose_MouseUp(object sender, MouseButtonEventArgs e) + { + if (!closing) + { + closing = true; + fadeOutStoryboard.Begin(); + } + } - private void fadeOutStoryboard_Completed(object sender, EventArgs e) { - Close(); - } + private void fadeOutStoryboard_Completed(object sender, EventArgs e) + { + Close(); + } - public void Show(string title, string subTitle, string icopath) { - tbTitle.Text = title; - tbSubTitle.Text = subTitle; - if (string.IsNullOrEmpty(subTitle)) - { - tbSubTitle.Visibility = Visibility.Collapsed; - } - if (!File.Exists(icopath)) { - imgIco.Source = new BitmapImage(new Uri("Images\\app.png", UriKind.Relative)); - } - else { - imgIco.Source = new BitmapImage(new Uri(icopath)); - } + public void Show(string title, string subTitle, string icopath) + { + tbTitle.Text = title; + tbSubTitle.Text = subTitle; + if (string.IsNullOrEmpty(subTitle)) + { + tbSubTitle.Visibility = Visibility.Collapsed; + } + if (!File.Exists(icopath)) + { + imgIco.Source = new BitmapImage(new Uri("Images\\app.png", UriKind.Relative)); + } + else { + imgIco.Source = new BitmapImage(new Uri(icopath)); + } - Show(); + Show(); - Dispatcher.DelayInvoke("ShowMsg", - () => { - if (!closing) { - closing = true; - Dispatcher.Invoke(new Action(fadeOutStoryboard.Begin)); - } - }, TimeSpan.FromSeconds(3)); - } - } + Dispatcher.InvokeAsync(async () => + { + if (!closing) + { + closing = true; + await Dispatcher.InvokeAsync(fadeOutStoryboard.Begin); + } + }); + } + } } diff --git a/Wox/ResultPanel.xaml.cs b/Wox/ResultPanel.xaml.cs index 534a561c97..16b5286f16 100644 --- a/Wox/ResultPanel.xaml.cs +++ b/Wox/ResultPanel.xaml.cs @@ -266,10 +266,10 @@ namespace Wox if (e.AddedItems.Count > 0 && e.AddedItems[0] != null) { lbResults.ScrollIntoView(e.AddedItems[0]); - Dispatcher.DelayInvoke("UpdateItemNumber", () => - { - UpdateItemNumber(); - }, TimeSpan.FromMilliseconds(3)); + //Dispatcher.DelayInvoke("UpdateItemNumber", () => + //{ + //UpdateItemNumber(); + //}, TimeSpan.FromMilliseconds(3)); } } diff --git a/Wox/Wox.csproj b/Wox/Wox.csproj index 52f54db55c..d05cf6ffd1 100644 --- a/Wox/Wox.csproj +++ b/Wox/Wox.csproj @@ -126,14 +126,6 @@ - - - - - - - - @@ -163,7 +155,6 @@ CustomQueryHotkeySetting.xaml - HotkeyControl.xaml