Replace DelayInvoke with Task + Async

This commit is contained in:
bao-qian
2016-01-06 06:31:17 +00:00
parent b78e0144de
commit 1a8efdbf2c
6 changed files with 88 additions and 137 deletions

View File

@@ -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);
}
}
}

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Input; using System.Windows.Input;
@@ -39,10 +40,10 @@ namespace Wox
SpecialKeyState specialKeyState = GlobalHotkey.Instance.CheckModifiers(); SpecialKeyState specialKeyState = GlobalHotkey.Instance.CheckModifiers();
var hotkeyModel = new HotkeyModel( var hotkeyModel = new HotkeyModel(
specialKeyState.AltPressed, specialKeyState.AltPressed,
specialKeyState.ShiftPressed, specialKeyState.ShiftPressed,
specialKeyState.WinPressed, specialKeyState.WinPressed,
specialKeyState.CtrlPressed, specialKeyState.CtrlPressed,
key); key);
var hotkeyString = hotkeyModel.ToString(); var hotkeyString = hotkeyModel.ToString();
@@ -52,12 +53,11 @@ namespace Wox
return; return;
} }
Dispatcher.DelayInvoke("HotkeyAvailabilityTest", Dispatcher.InvokeAsync(async () =>
() => {
{ await Task.Delay(500);
SetHotkey(hotkeyModel); SetHotkey(hotkeyModel);
}, });
TimeSpan.FromMilliseconds(500));
} }
public void SetHotkey(HotkeyModel keyModel, bool triggerValidate = true) public void SetHotkey(HotkeyModel keyModel, bool triggerValidate = true)
@@ -94,7 +94,7 @@ namespace Wox
{ {
try try
{ {
HotkeyManager.Current.AddOrReplace("HotkeyAvailabilityTest", CurrentHotkey.CharKey, CurrentHotkey.ModifierKeys, (sender, e) => {}); HotkeyManager.Current.AddOrReplace("HotkeyAvailabilityTest", CurrentHotkey.CharKey, CurrentHotkey.ModifierKeys, (sender, e) => { });
return true; return true;
} }

View File

@@ -7,6 +7,7 @@ using System.Linq;
using System.Net; using System.Net;
using System.Reflection; using System.Reflection;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Input; using System.Windows.Input;
@@ -484,13 +485,14 @@ namespace Wox
} }
} }
_lastQuery = query; _lastQuery = query;
Dispatcher.DelayInvoke("ShowProgressbar", () => Dispatcher.InvokeAsync(async () =>
{ {
await Task.Delay(150);
if (!string.IsNullOrEmpty(query.RawQuery) && query.RawQuery == _lastQuery.RawQuery && !_queryHasReturn) if (!string.IsNullOrEmpty(query.RawQuery) && query.RawQuery == _lastQuery.RawQuery && !_queryHasReturn)
{ {
StartProgress(); StartProgress();
} }
}, TimeSpan.FromMilliseconds(150)); });
PluginManager.QueryForAllPlugins(query); PluginManager.QueryForAllPlugins(query);
} }
StopProgress(); StopProgress();
@@ -829,6 +831,7 @@ namespace Wox
private void UpdateResultView(List<Result> list, PluginMetadata metadata, Query originQuery) private void UpdateResultView(List<Result> list, PluginMetadata metadata, Query originQuery)
{ {
Thread.Sleep(3000);
_queryHasReturn = true; _queryHasReturn = true;
progressBar.Dispatcher.Invoke(new Action(StopProgress)); progressBar.Dispatcher.Invoke(new Action(StopProgress));

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.IO; using System.IO;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Forms; using System.Windows.Forms;
using System.Windows.Input; using System.Windows.Input;
@@ -7,71 +8,81 @@ using System.Windows.Media.Animation;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using Wox.Helper; using Wox.Helper;
namespace Wox { namespace Wox
public partial class Msg : Window { {
Storyboard fadeOutStoryboard = new Storyboard(); public partial class Msg : Window
private bool closing = false; {
Storyboard fadeOutStoryboard = new Storyboard();
private bool closing = false;
public Msg() { public Msg()
InitializeComponent(); {
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position); InitializeComponent();
var dipWorkingArea = WindowIntelopHelper.TransformPixelsToDIP(this, var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
var dipWorkingArea = WindowIntelopHelper.TransformPixelsToDIP(this,
screen.WorkingArea.Width, screen.WorkingArea.Width,
screen.WorkingArea.Height); screen.WorkingArea.Height);
Left = dipWorkingArea.X - this.Width; Left = dipWorkingArea.X - this.Width;
Top = dipWorkingArea.Y; Top = dipWorkingArea.Y;
showAnimation.From = dipWorkingArea.Y; showAnimation.From = dipWorkingArea.Y;
showAnimation.To = dipWorkingArea.Y - Height; showAnimation.To = dipWorkingArea.Y - Height;
// Create the fade out storyboard // Create the fade out storyboard
fadeOutStoryboard.Completed += new EventHandler(fadeOutStoryboard_Completed); fadeOutStoryboard.Completed += new EventHandler(fadeOutStoryboard_Completed);
DoubleAnimation fadeOutAnimation = new DoubleAnimation(dipWorkingArea.Y - Height, dipWorkingArea.Y, new Duration(TimeSpan.FromSeconds(0.3))) { DoubleAnimation fadeOutAnimation = new DoubleAnimation(dipWorkingArea.Y - Height, dipWorkingArea.Y, new Duration(TimeSpan.FromSeconds(0.3)))
AccelerationRatio = 0.2 {
}; AccelerationRatio = 0.2
Storyboard.SetTarget(fadeOutAnimation, this); };
Storyboard.SetTargetProperty(fadeOutAnimation, new PropertyPath(TopProperty)); Storyboard.SetTarget(fadeOutAnimation, this);
fadeOutStoryboard.Children.Add(fadeOutAnimation); 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("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.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.MouseUp += imgClose_MouseUp;
} }
void imgClose_MouseUp(object sender, MouseButtonEventArgs e) { void imgClose_MouseUp(object sender, MouseButtonEventArgs e)
if (!closing) { {
closing = true; if (!closing)
fadeOutStoryboard.Begin(); {
} closing = true;
} fadeOutStoryboard.Begin();
}
}
private void fadeOutStoryboard_Completed(object sender, EventArgs e) { private void fadeOutStoryboard_Completed(object sender, EventArgs e)
Close(); {
} Close();
}
public void Show(string title, string subTitle, string icopath) { public void Show(string title, string subTitle, string icopath)
tbTitle.Text = title; {
tbSubTitle.Text = subTitle; tbTitle.Text = title;
if (string.IsNullOrEmpty(subTitle)) tbSubTitle.Text = subTitle;
{ if (string.IsNullOrEmpty(subTitle))
tbSubTitle.Visibility = Visibility.Collapsed; {
} tbSubTitle.Visibility = Visibility.Collapsed;
if (!File.Exists(icopath)) { }
imgIco.Source = new BitmapImage(new Uri("Images\\app.png", UriKind.Relative)); if (!File.Exists(icopath))
} {
else { imgIco.Source = new BitmapImage(new Uri("Images\\app.png", UriKind.Relative));
imgIco.Source = new BitmapImage(new Uri(icopath)); }
} else {
imgIco.Source = new BitmapImage(new Uri(icopath));
}
Show(); Show();
Dispatcher.DelayInvoke("ShowMsg", Dispatcher.InvokeAsync(async () =>
() => { {
if (!closing) { if (!closing)
closing = true; {
Dispatcher.Invoke(new Action(fadeOutStoryboard.Begin)); closing = true;
} await Dispatcher.InvokeAsync(fadeOutStoryboard.Begin);
}, TimeSpan.FromSeconds(3)); }
} });
} }
}
} }

View File

@@ -266,10 +266,10 @@ namespace Wox
if (e.AddedItems.Count > 0 && e.AddedItems[0] != null) if (e.AddedItems.Count > 0 && e.AddedItems[0] != null)
{ {
lbResults.ScrollIntoView(e.AddedItems[0]); lbResults.ScrollIntoView(e.AddedItems[0]);
Dispatcher.DelayInvoke("UpdateItemNumber", () => //Dispatcher.DelayInvoke("UpdateItemNumber", () =>
{ //{
UpdateItemNumber(); //UpdateItemNumber();
}, TimeSpan.FromMilliseconds(3)); //}, TimeSpan.FromMilliseconds(3));
} }
} }

View File

@@ -126,14 +126,6 @@
<Compile Include="Helper\ListBoxItems.cs" /> <Compile Include="Helper\ListBoxItems.cs" />
<Compile Include="Helper\SingletonWindowOpener.cs" /> <Compile Include="Helper\SingletonWindowOpener.cs" />
<Compile Include="ImageLoader\ImageCacheStroage.cs" /> <Compile Include="ImageLoader\ImageCacheStroage.cs" />
<Compile Include="ShellContext\ShellContextMenuManager.cs" />
<Compile Include="ShellContext\ShellAPI.cs" />
<Compile Include="ShellContext\Enums.cs" />
<Compile Include="ShellContext\Guids.cs" />
<Compile Include="ShellContext\IContextMenu.cs" />
<Compile Include="ShellContext\IEnumIDList.cs" />
<Compile Include="ShellContext\IShellFolder.cs" />
<Compile Include="ShellContext\Structs.cs" />
<Compile Include="Storage\QueryHistoryStorage.cs" /> <Compile Include="Storage\QueryHistoryStorage.cs" />
<Compile Include="Storage\TopMostRecordStorage.cs" /> <Compile Include="Storage\TopMostRecordStorage.cs" />
<Compile Include="Storage\UserSelectedRecordStorage.cs" /> <Compile Include="Storage\UserSelectedRecordStorage.cs" />
@@ -163,7 +155,6 @@
<Compile Include="CustomQueryHotkeySetting.xaml.cs"> <Compile Include="CustomQueryHotkeySetting.xaml.cs">
<DependentUpon>CustomQueryHotkeySetting.xaml</DependentUpon> <DependentUpon>CustomQueryHotkeySetting.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="Helper\DispatcherExtensions.cs" />
<Compile Include="Helper\DWMDropShadow.cs" /> <Compile Include="Helper\DWMDropShadow.cs" />
<Compile Include="HotkeyControl.xaml.cs"> <Compile Include="HotkeyControl.xaml.cs">
<DependentUpon>HotkeyControl.xaml</DependentUpon> <DependentUpon>HotkeyControl.xaml</DependentUpon>