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

Commit

Permalink
remove nito.async dependency by implementing own async queue
Browse files Browse the repository at this point in the history
  • Loading branch information
justcoding121 committed Apr 13, 2018
1 parent 1733016 commit a92b595
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 143 deletions.
7 changes: 7 additions & 0 deletions EventHook.Examples/EventHook.ConsoleApp.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ static void Main(string[] args)


Console.Read();

keyboardWatcher.Stop();
mouseWatcher.Stop();
clipboardWatcher.Stop();
applicationWatcher.Stop();
printWatcher.Stop();

eventHookFactory.Dispose();
}

Expand Down
14 changes: 13 additions & 1 deletion EventHook.Examples/EventHook.WPF.Example/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ namespace EventHook.WPF.Example
/// </summary>
public partial class MainWindow : Window
{
EventHookFactory eventHookFactory = new EventHookFactory();
private EventHookFactory eventHookFactory = new EventHookFactory();

private ApplicationWatcher applicationWatcher;
private KeyboardWatcher keyboardWatcher;
private MouseWatcher mouseWatcher;
private ClipboardWatcher clipboardWatcher;
private PrintWatcher printWatcher;

public MainWindow()
{
Expand Down Expand Up @@ -57,6 +63,12 @@ public MainWindow()

private void OnApplicationExit(object sender, EventArgs e)
{
keyboardWatcher.Stop();
mouseWatcher.Stop();
clipboardWatcher.Stop();
applicationWatcher.Stop();
printWatcher.Stop();

eventHookFactory.Dispose();
}
}
Expand Down
14 changes: 13 additions & 1 deletion EventHook.Examples/EventHook.WinForms.Example/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ namespace EventHook.WinForms.Example
{
public partial class MainForm : Form
{
EventHookFactory eventHookFactory = new EventHookFactory();
private EventHookFactory eventHookFactory = new EventHookFactory();

private ApplicationWatcher applicationWatcher;
private KeyboardWatcher keyboardWatcher;
private MouseWatcher mouseWatcher;
private ClipboardWatcher clipboardWatcher;
private PrintWatcher printWatcher;

public MainForm()
{
Expand Down Expand Up @@ -60,6 +66,12 @@ public MainForm()

private void OnApplicationExit(object sender, EventArgs e)
{
keyboardWatcher.Stop();
mouseWatcher.Stop();
clipboardWatcher.Stop();
applicationWatcher.Stop();
printWatcher.Stop();

eventHookFactory.Dispose();
}
}
Expand Down
32 changes: 16 additions & 16 deletions EventHook/ApplicationWatcher.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using EventHook.Hooks;
using EventHook.Helpers;
using Nito.AsyncEx;
using System.Threading.Tasks;
using System.Threading;

Expand Down Expand Up @@ -47,24 +45,25 @@ public class ApplicationEventArgs : EventArgs
/// </summary>
public class ApplicationWatcher
{
/*Application history*/
private object accesslock = new object();
private bool isRunning;

private AsyncCollection<object> appQueue;
private SyncFactory factory;
private AsyncQueue<object> appQueue;
private CancellationTokenSource taskCancellationTokenSource;

private Dictionary<IntPtr, WindowData> activeWindows;
private DateTime prevTimeApp;

public event EventHandler<ApplicationEventArgs> OnApplicationWindowChange;

private SyncFactory factory;
private WindowHook windowHook;

internal ApplicationWatcher(SyncFactory factory)
{
this.factory = factory;
}

/// <summary>
/// Start to watch
/// </summary>
Expand All @@ -76,8 +75,9 @@ public void Start()
{
activeWindows = new Dictionary<IntPtr, WindowData>();
prevTimeApp = DateTime.Now;

appQueue = new AsyncCollection<object>();

var taskCancellationTokenSource = new CancellationTokenSource();
appQueue = new AsyncQueue<object>(taskCancellationTokenSource.Token);

//This needs to run on UI thread context
//So use task factory with the shared UI message pump thread
Expand Down Expand Up @@ -111,7 +111,6 @@ public void Stop()
{
if (isRunning)
{

//This needs to run on UI thread context
//So use task factory with the shared UI message pump thread
Task.Factory.StartNew(() =>
Expand All @@ -125,8 +124,9 @@ public void Stop()
TaskCreationOptions.None,
factory.GetTaskScheduler()).Wait();

appQueue.Add(false);
appQueue.Enqueue(false);
isRunning = false;
taskCancellationTokenSource.Cancel();
}
}

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

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

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

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

var wnd = (WindowData)item;
Expand Down Expand Up @@ -236,16 +236,16 @@ private void WindowActivated(WindowData wnd)
/// <param name="wnd"></param>
private void WindowDestroyed(WindowData wnd)
{
if(activeWindows.ContainsKey(wnd.HWnd))
if (activeWindows.ContainsKey(wnd.HWnd))
{
ApplicationStatus(activeWindows[wnd.HWnd], ApplicationEvents.Closed);
activeWindows.Remove(wnd.HWnd);
}

lastEventWasLaunched = false;
}



/// <summary>
/// invoke user call back
Expand Down
37 changes: 20 additions & 17 deletions EventHook/ClipboardWatcher.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using EventHook.Hooks;
using EventHook.Helpers;
using Nito.AsyncEx;
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
Expand Down Expand Up @@ -35,31 +34,34 @@ public class ClipboardEventArgs : EventArgs
/// </summary>
public class ClipboardWatcher
{
/*Clip board monitor*/
public bool isRunning;
private object accesslock = new object();
private object accesslock = new object();
public bool isRunning;

private ClipBoardHook clip;
private AsyncCollection<object> clipQueue;
private SyncFactory factory;
public event EventHandler<ClipboardEventArgs> OnClipboardModified;
private AsyncQueue<object> clipQueue;
private CancellationTokenSource taskCancellationTokenSource;

private ClipBoardHook clip;
public event EventHandler<ClipboardEventArgs> OnClipboardModified;

internal ClipboardWatcher(SyncFactory factory)
{
this.factory = factory;
}

/// <summary>
/// Start watching
/// </summary>
public void Start()
public void Start()
{

lock (accesslock)
{
if (!isRunning)
{
clipQueue = new AsyncCollection<object>();

taskCancellationTokenSource = new CancellationTokenSource();
clipQueue = new AsyncQueue<object>(taskCancellationTokenSource.Token);

//This needs to run on UI thread context
//So use task factory with the shared UI message pump thread
Task.Factory.StartNew(() =>
Expand All @@ -83,7 +85,7 @@ public void Start()
/// <summary>
/// Stop watching
/// </summary>
public void Stop()
public void Stop()
{

lock (accesslock)
Expand All @@ -106,7 +108,8 @@ public void Stop()
}

isRunning = false;
clipQueue.Add(false);
clipQueue.Enqueue(false);
taskCancellationTokenSource.Cancel();
}
}

Expand All @@ -117,20 +120,20 @@ public void Stop()
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ClipboardHandler(object sender, EventArgs e)
private void ClipboardHandler(object sender, EventArgs e)
{
clipQueue.Add(sender);
clipQueue.Enqueue(sender);
}

/// <summary>
/// Consume event from producer queue asynchronously
/// </summary>
/// <returns></returns>
private async Task ClipConsumerAsync()
private async Task ClipConsumerAsync()
{
while (isRunning)
{
var item = await clipQueue.TakeAsync();
var item = await clipQueue.DequeueAsync();
if (item is bool) break;

ClipboardHandler(item);
Expand All @@ -142,7 +145,7 @@ private async Task ClipConsumerAsync()
/// Actual handler to invoke user call backs
/// </summary>
/// <param name="sender"></param>
private void ClipboardHandler(object sender)
private void ClipboardHandler(object sender)
{
IDataObject iData = (DataObject)sender;

Expand Down
14 changes: 1 addition & 13 deletions EventHook/EventHook.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,6 @@
<Reference Include="Microsoft.Office.Interop.Outlook, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL">
<EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>
<Reference Include="Nito.AsyncEx, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nito.AsyncEx.Concurrent, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Concurrent.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nito.AsyncEx.Enlightenment, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Enlightenment.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System" />
Expand All @@ -77,6 +65,7 @@
<Compile Include="ApplicationWatcher.cs" />
<Compile Include="ClipboardWatcher.cs" />
<Compile Include="EventHookFactory.cs" />
<Compile Include="Helpers\AsyncQueue.cs" />
<Compile Include="Helpers\SyncFactory.cs" />
<Compile Include="KeyboardWatcher.cs" />
<Compile Include="MouseWatcher.cs" />
Expand All @@ -100,7 +89,6 @@
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
Expand Down
5 changes: 0 additions & 5 deletions EventHook/EventHook.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
<licenseUrl>https://github.com/titanium007/Windows-User-Action-Hook/blob/master/LICENSE</licenseUrl>
<authors>titanium007</authors>
<owners></owners>
<dependencies>
<group targetFramework="net45">
<dependency id="Nito.AsyncEx" version="3.0.1" />
</group>
</dependencies>
</metadata>
<files>
<file src="bin\$configuration$\EventHook.dll" target="lib\net45" />
Expand Down
Loading

0 comments on commit a92b595

Please sign in to comment.