Skip to content
This repository was archived by the owner on Apr 21, 2021. It is now read-only.

Commit a92b595

Browse files
committed
remove nito.async dependency by implementing own async queue
1 parent 1733016 commit a92b595

File tree

14 files changed

+201
-143
lines changed

14 files changed

+201
-143
lines changed

EventHook.Examples/EventHook.ConsoleApp.Example/Program.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ static void Main(string[] args)
4747

4848

4949
Console.Read();
50+
51+
keyboardWatcher.Stop();
52+
mouseWatcher.Stop();
53+
clipboardWatcher.Stop();
54+
applicationWatcher.Stop();
55+
printWatcher.Stop();
56+
5057
eventHookFactory.Dispose();
5158
}
5259

EventHook.Examples/EventHook.WPF.Example/MainWindow.xaml.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ namespace EventHook.WPF.Example
99
/// </summary>
1010
public partial class MainWindow : Window
1111
{
12-
EventHookFactory eventHookFactory = new EventHookFactory();
12+
private EventHookFactory eventHookFactory = new EventHookFactory();
13+
14+
private ApplicationWatcher applicationWatcher;
15+
private KeyboardWatcher keyboardWatcher;
16+
private MouseWatcher mouseWatcher;
17+
private ClipboardWatcher clipboardWatcher;
18+
private PrintWatcher printWatcher;
1319

1420
public MainWindow()
1521
{
@@ -57,6 +63,12 @@ public MainWindow()
5763

5864
private void OnApplicationExit(object sender, EventArgs e)
5965
{
66+
keyboardWatcher.Stop();
67+
mouseWatcher.Stop();
68+
clipboardWatcher.Stop();
69+
applicationWatcher.Stop();
70+
printWatcher.Stop();
71+
6072
eventHookFactory.Dispose();
6173
}
6274
}

EventHook.Examples/EventHook.WinForms.Example/MainForm.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ namespace EventHook.WinForms.Example
1313
{
1414
public partial class MainForm : Form
1515
{
16-
EventHookFactory eventHookFactory = new EventHookFactory();
16+
private EventHookFactory eventHookFactory = new EventHookFactory();
17+
18+
private ApplicationWatcher applicationWatcher;
19+
private KeyboardWatcher keyboardWatcher;
20+
private MouseWatcher mouseWatcher;
21+
private ClipboardWatcher clipboardWatcher;
22+
private PrintWatcher printWatcher;
1723

1824
public MainForm()
1925
{
@@ -60,6 +66,12 @@ public MainForm()
6066

6167
private void OnApplicationExit(object sender, EventArgs e)
6268
{
69+
keyboardWatcher.Stop();
70+
mouseWatcher.Stop();
71+
clipboardWatcher.Stop();
72+
applicationWatcher.Stop();
73+
printWatcher.Stop();
74+
6375
eventHookFactory.Dispose();
6476
}
6577
}

EventHook/ApplicationWatcher.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using EventHook.Hooks;
54
using EventHook.Helpers;
6-
using Nito.AsyncEx;
75
using System.Threading.Tasks;
86
using System.Threading;
97

@@ -47,24 +45,25 @@ public class ApplicationEventArgs : EventArgs
4745
/// </summary>
4846
public class ApplicationWatcher
4947
{
50-
/*Application history*/
5148
private object accesslock = new object();
5249
private bool isRunning;
5350

54-
private AsyncCollection<object> appQueue;
51+
private SyncFactory factory;
52+
private AsyncQueue<object> appQueue;
53+
private CancellationTokenSource taskCancellationTokenSource;
5554

5655
private Dictionary<IntPtr, WindowData> activeWindows;
5756
private DateTime prevTimeApp;
5857

5958
public event EventHandler<ApplicationEventArgs> OnApplicationWindowChange;
6059

61-
private SyncFactory factory;
6260
private WindowHook windowHook;
6361

6462
internal ApplicationWatcher(SyncFactory factory)
6563
{
6664
this.factory = factory;
6765
}
66+
6867
/// <summary>
6968
/// Start to watch
7069
/// </summary>
@@ -76,8 +75,9 @@ public void Start()
7675
{
7776
activeWindows = new Dictionary<IntPtr, WindowData>();
7877
prevTimeApp = DateTime.Now;
79-
80-
appQueue = new AsyncCollection<object>();
78+
79+
var taskCancellationTokenSource = new CancellationTokenSource();
80+
appQueue = new AsyncQueue<object>(taskCancellationTokenSource.Token);
8181

8282
//This needs to run on UI thread context
8383
//So use task factory with the shared UI message pump thread
@@ -111,7 +111,6 @@ public void Stop()
111111
{
112112
if (isRunning)
113113
{
114-
115114
//This needs to run on UI thread context
116115
//So use task factory with the shared UI message pump thread
117116
Task.Factory.StartNew(() =>
@@ -125,8 +124,9 @@ public void Stop()
125124
TaskCreationOptions.None,
126125
factory.GetTaskScheduler()).Wait();
127126

128-
appQueue.Add(false);
127+
appQueue.Enqueue(false);
129128
isRunning = false;
129+
taskCancellationTokenSource.Cancel();
130130
}
131131
}
132132

@@ -139,7 +139,7 @@ public void Stop()
139139
/// <param name="hWnd"></param>
140140
private void WindowCreated(ShellHook shellObject, IntPtr hWnd)
141141
{
142-
appQueue.Add(new WindowData() { HWnd = hWnd, EventType = 0 });
142+
appQueue.Enqueue(new WindowData() { HWnd = hWnd, EventType = 0 });
143143
}
144144

145145
/// <summary>
@@ -149,7 +149,7 @@ private void WindowCreated(ShellHook shellObject, IntPtr hWnd)
149149
/// <param name="hWnd"></param>
150150
private void WindowDestroyed(ShellHook shellObject, IntPtr hWnd)
151151
{
152-
appQueue.Add(new WindowData() { HWnd = hWnd, EventType = 2 });
152+
appQueue.Enqueue(new WindowData() { HWnd = hWnd, EventType = 2 });
153153
}
154154

155155
/// <summary>
@@ -159,7 +159,7 @@ private void WindowDestroyed(ShellHook shellObject, IntPtr hWnd)
159159
/// <param name="hWnd"></param>
160160
private void WindowActivated(ShellHook shellObject, IntPtr hWnd)
161161
{
162-
appQueue.Add(new WindowData() { HWnd = hWnd, EventType = 1 });
162+
appQueue.Enqueue(new WindowData() { HWnd = hWnd, EventType = 1 });
163163
}
164164

165165
/// <summary>
@@ -174,7 +174,7 @@ private async Task AppConsumer()
174174
while (isRunning)
175175
{
176176
//blocking here until a key is added to the queue
177-
var item = await appQueue.TakeAsync();
177+
var item = await appQueue.DequeueAsync();
178178
if (item is bool) break;
179179

180180
var wnd = (WindowData)item;
@@ -236,16 +236,16 @@ private void WindowActivated(WindowData wnd)
236236
/// <param name="wnd"></param>
237237
private void WindowDestroyed(WindowData wnd)
238238
{
239-
if(activeWindows.ContainsKey(wnd.HWnd))
239+
if (activeWindows.ContainsKey(wnd.HWnd))
240240
{
241241
ApplicationStatus(activeWindows[wnd.HWnd], ApplicationEvents.Closed);
242242
activeWindows.Remove(wnd.HWnd);
243243
}
244-
244+
245245
lastEventWasLaunched = false;
246246
}
247247

248-
248+
249249

250250
/// <summary>
251251
/// invoke user call back

EventHook/ClipboardWatcher.cs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using EventHook.Hooks;
22
using EventHook.Helpers;
3-
using Nito.AsyncEx;
43
using System;
54
using System.Threading.Tasks;
65
using System.Windows.Forms;
@@ -35,31 +34,34 @@ public class ClipboardEventArgs : EventArgs
3534
/// </summary>
3635
public class ClipboardWatcher
3736
{
38-
/*Clip board monitor*/
39-
public bool isRunning;
40-
private object accesslock = new object();
37+
private object accesslock = new object();
38+
public bool isRunning;
4139

42-
private ClipBoardHook clip;
43-
private AsyncCollection<object> clipQueue;
4440
private SyncFactory factory;
45-
public event EventHandler<ClipboardEventArgs> OnClipboardModified;
41+
private AsyncQueue<object> clipQueue;
42+
private CancellationTokenSource taskCancellationTokenSource;
43+
44+
private ClipBoardHook clip;
45+
public event EventHandler<ClipboardEventArgs> OnClipboardModified;
4646

4747
internal ClipboardWatcher(SyncFactory factory)
4848
{
4949
this.factory = factory;
5050
}
51+
5152
/// <summary>
5253
/// Start watching
5354
/// </summary>
54-
public void Start()
55+
public void Start()
5556
{
5657

5758
lock (accesslock)
5859
{
5960
if (!isRunning)
6061
{
61-
clipQueue = new AsyncCollection<object>();
62-
62+
taskCancellationTokenSource = new CancellationTokenSource();
63+
clipQueue = new AsyncQueue<object>(taskCancellationTokenSource.Token);
64+
6365
//This needs to run on UI thread context
6466
//So use task factory with the shared UI message pump thread
6567
Task.Factory.StartNew(() =>
@@ -83,7 +85,7 @@ public void Start()
8385
/// <summary>
8486
/// Stop watching
8587
/// </summary>
86-
public void Stop()
88+
public void Stop()
8789
{
8890

8991
lock (accesslock)
@@ -106,7 +108,8 @@ public void Stop()
106108
}
107109

108110
isRunning = false;
109-
clipQueue.Add(false);
111+
clipQueue.Enqueue(false);
112+
taskCancellationTokenSource.Cancel();
110113
}
111114
}
112115

@@ -117,20 +120,20 @@ public void Stop()
117120
/// </summary>
118121
/// <param name="sender"></param>
119122
/// <param name="e"></param>
120-
private void ClipboardHandler(object sender, EventArgs e)
123+
private void ClipboardHandler(object sender, EventArgs e)
121124
{
122-
clipQueue.Add(sender);
125+
clipQueue.Enqueue(sender);
123126
}
124127

125128
/// <summary>
126129
/// Consume event from producer queue asynchronously
127130
/// </summary>
128131
/// <returns></returns>
129-
private async Task ClipConsumerAsync()
132+
private async Task ClipConsumerAsync()
130133
{
131134
while (isRunning)
132135
{
133-
var item = await clipQueue.TakeAsync();
136+
var item = await clipQueue.DequeueAsync();
134137
if (item is bool) break;
135138

136139
ClipboardHandler(item);
@@ -142,7 +145,7 @@ private async Task ClipConsumerAsync()
142145
/// Actual handler to invoke user call backs
143146
/// </summary>
144147
/// <param name="sender"></param>
145-
private void ClipboardHandler(object sender)
148+
private void ClipboardHandler(object sender)
146149
{
147150
IDataObject iData = (DataObject)sender;
148151

EventHook/EventHook.csproj

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,6 @@
4242
<Reference Include="Microsoft.Office.Interop.Outlook, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL">
4343
<EmbedInteropTypes>True</EmbedInteropTypes>
4444
</Reference>
45-
<Reference Include="Nito.AsyncEx, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
46-
<HintPath>..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.dll</HintPath>
47-
<Private>True</Private>
48-
</Reference>
49-
<Reference Include="Nito.AsyncEx.Concurrent, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
50-
<HintPath>..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Concurrent.dll</HintPath>
51-
<Private>True</Private>
52-
</Reference>
53-
<Reference Include="Nito.AsyncEx.Enlightenment, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
54-
<HintPath>..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Enlightenment.dll</HintPath>
55-
<Private>True</Private>
56-
</Reference>
5745
<Reference Include="PresentationCore" />
5846
<Reference Include="PresentationFramework" />
5947
<Reference Include="System" />
@@ -77,6 +65,7 @@
7765
<Compile Include="ApplicationWatcher.cs" />
7866
<Compile Include="ClipboardWatcher.cs" />
7967
<Compile Include="EventHookFactory.cs" />
68+
<Compile Include="Helpers\AsyncQueue.cs" />
8069
<Compile Include="Helpers\SyncFactory.cs" />
8170
<Compile Include="KeyboardWatcher.cs" />
8271
<Compile Include="MouseWatcher.cs" />
@@ -100,7 +89,6 @@
10089
</ItemGroup>
10190
<ItemGroup>
10291
<None Include="app.config" />
103-
<None Include="packages.config" />
10492
</ItemGroup>
10593
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
10694
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />

EventHook/EventHook.nuspec

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010
<licenseUrl>https://github.com/titanium007/Windows-User-Action-Hook/blob/master/LICENSE</licenseUrl>
1111
<authors>titanium007</authors>
1212
<owners></owners>
13-
<dependencies>
14-
<group targetFramework="net45">
15-
<dependency id="Nito.AsyncEx" version="3.0.1" />
16-
</group>
17-
</dependencies>
1813
</metadata>
1914
<files>
2015
<file src="bin\$configuration$\EventHook.dll" target="lib\net45" />

0 commit comments

Comments
 (0)