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

Commit 275b240

Browse files
committed
issue #3 experimental fix
use main window handle/sync context if available
1 parent 1edeca6 commit 275b240

26 files changed

+540
-34
lines changed

EventHook.sln

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 2013
4-
VisualStudioVersion = 12.0.31101.0
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.24720.0
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventHook", "EventHook\EventHook.csproj", "{4FF5E04F-3425-4F55-A50A-30CEBA7C2B95}"
77
EndProject
8-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{C622ACD2-76FB-4B6D-844D-C0C406C73214}"
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{C622ACD2-76FB-4B6D-844D-C0C406C73214}"
99
EndProject
1010
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{44D5BA9F-DF7B-4208-BCD0-A909ECAADE1C}"
1111
ProjectSection(SolutionItems) = preProject
@@ -14,7 +14,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{44D5BA
1414
.nuget\NuGet.targets = .nuget\NuGet.targets
1515
EndProjectSection
1616
EndProject
17-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventHook.Tests", "EventHook.Tests\EventHook.Tests.csproj", "{0075830E-89B6-4E6B-9C49-CA377746AB4C}"
17+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventHook.Forms.Tests", "Examples\EventHook.Forms.Tests\EventHook.Forms.Tests.csproj", "{17264D8C-CF5A-4032-9E70-090525D574F2}"
18+
EndProject
19+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventHook.Tests", "Examples\EventHook.Tests\EventHook.Tests.csproj", "{0075830E-89B6-4E6B-9C49-CA377746AB4C}"
1820
EndProject
1921
Global
2022
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -32,6 +34,14 @@ Global
3234
{4FF5E04F-3425-4F55-A50A-30CEBA7C2B95}.Release|Any CPU.Build.0 = Release|Any CPU
3335
{4FF5E04F-3425-4F55-A50A-30CEBA7C2B95}.Release-Net45|Any CPU.ActiveCfg = Release-Net45|Any CPU
3436
{4FF5E04F-3425-4F55-A50A-30CEBA7C2B95}.Release-Net45|Any CPU.Build.0 = Release-Net45|Any CPU
37+
{17264D8C-CF5A-4032-9E70-090525D574F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
38+
{17264D8C-CF5A-4032-9E70-090525D574F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
39+
{17264D8C-CF5A-4032-9E70-090525D574F2}.Debug-Net45|Any CPU.ActiveCfg = Debug|Any CPU
40+
{17264D8C-CF5A-4032-9E70-090525D574F2}.Debug-Net45|Any CPU.Build.0 = Debug|Any CPU
41+
{17264D8C-CF5A-4032-9E70-090525D574F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
42+
{17264D8C-CF5A-4032-9E70-090525D574F2}.Release|Any CPU.Build.0 = Release|Any CPU
43+
{17264D8C-CF5A-4032-9E70-090525D574F2}.Release-Net45|Any CPU.ActiveCfg = Release|Any CPU
44+
{17264D8C-CF5A-4032-9E70-090525D574F2}.Release-Net45|Any CPU.Build.0 = Release|Any CPU
3545
{0075830E-89B6-4E6B-9C49-CA377746AB4C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
3646
{0075830E-89B6-4E6B-9C49-CA377746AB4C}.Debug|Any CPU.Build.0 = Debug|Any CPU
3747
{0075830E-89B6-4E6B-9C49-CA377746AB4C}.Debug-Net45|Any CPU.ActiveCfg = Debug-Net45|Any CPU
@@ -45,6 +55,7 @@ Global
4555
HideSolutionNode = FALSE
4656
EndGlobalSection
4757
GlobalSection(NestedProjects) = preSolution
58+
{17264D8C-CF5A-4032-9E70-090525D574F2} = {C622ACD2-76FB-4B6D-844D-C0C406C73214}
4859
{0075830E-89B6-4E6B-9C49-CA377746AB4C} = {C622ACD2-76FB-4B6D-844D-C0C406C73214}
4960
EndGlobalSection
5061
EndGlobal

EventHook/ApplicationWatcher.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Collections.Concurrent;
55
using System.Threading;
66
using EventHook.Hooks;
7-
using EventHook.Hooks.Helpers;
7+
using EventHook.Helpers;
88
using Nito.AsyncEx;
99
using System.Threading.Tasks;
1010

@@ -40,7 +40,7 @@ public class ApplicationWatcher
4040
private static object _Accesslock = new object();
4141
private static bool _IsRunning;
4242

43-
private static AsyncCollection<object> _appQueue;
43+
private static AsyncCollection<object> appQueue;
4444

4545
private static List<WindowData> _activeWindows;
4646
private static DateTime _prevTimeApp;
@@ -55,9 +55,7 @@ public static void Start()
5555
_activeWindows = new List<WindowData> { };
5656
_prevTimeApp = DateTime.Now;
5757

58-
_appQueue = new AsyncCollection<object>();
59-
60-
var handler = SharedMessagePump.GetHandle();
58+
appQueue = new AsyncCollection<object>();
6159

6260
Task.Factory.StartNew(() => { }).ContinueWith(x =>
6361
{
@@ -84,30 +82,30 @@ public static void Stop()
8482
WindowHook.WindowDestroyed -= new GeneralShellHookEventHandler(WindowDestroyed);
8583
WindowHook.WindowActivated -= new GeneralShellHookEventHandler(WindowActivated);
8684

87-
_appQueue.Add(false);
85+
appQueue.Add(false);
8886
_IsRunning = false;
8987
}
9088

9189
}
9290
private static void WindowCreated(ShellHook shellObject, IntPtr hWnd)
9391
{
94-
_appQueue.Add(new WindowData() { HWnd = hWnd, EventType = 0 });
92+
appQueue.Add(new WindowData() { HWnd = hWnd, EventType = 0 });
9593
}
9694
private static void WindowDestroyed(ShellHook shellObject, IntPtr hWnd)
9795
{
98-
_appQueue.Add(new WindowData() { HWnd = hWnd, EventType = 2 });
96+
appQueue.Add(new WindowData() { HWnd = hWnd, EventType = 2 });
9997
}
10098
private static void WindowActivated(ShellHook shellObject, IntPtr hWnd)
10199
{
102-
_appQueue.Add(new WindowData() { HWnd = hWnd, EventType = 1 });
100+
appQueue.Add(new WindowData() { HWnd = hWnd, EventType = 1 });
103101
}
104102
// This is the method to run when the timer is raised.
105103
private static async Task AppConsumer()
106104
{
107105
while (_IsRunning)
108106
{
109107
//blocking here until a key is added to the queue
110-
var item = await _appQueue.TakeAsync();
108+
var item = await appQueue.TakeAsync();
111109
if (item is bool) break;
112110

113111
var wnd = (WindowData)item;

EventHook/ClipboardWatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using EventHook.Hooks;
2-
using EventHook.Hooks.Helpers;
2+
using EventHook.Helpers;
33
using Nito.AsyncEx;
44
using System;
55
using System.Collections.Concurrent;

EventHook/EventHook.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@
144144
<ItemGroup>
145145
<Compile Include="ApplicationWatcher.cs" />
146146
<Compile Include="ClipboardWatcher.cs" />
147-
<Compile Include="Hooks\Helpers\SharedMessagePump.cs" />
147+
<Compile Include="Helpers\SharedMessagePump.cs" />
148148
<Compile Include="KeyboardWatcher.cs" />
149149
<Compile Include="MouseWatcher.cs" />
150150
<Compile Include="PrintWatcher.cs" />
151151
<Compile Include="Hooks\WindowHook.cs" />
152-
<Compile Include="Hooks\Helpers\WindowHelper.cs" />
152+
<Compile Include="Helpers\WindowHelper.cs" />
153153
<Compile Include="Hooks\ClipBoardHook.cs">
154154
<SubType>Form</SubType>
155155
</Compile>

EventHook/Hooks/Helpers/SharedMessagePump.cs renamed to EventHook/Helpers/SharedMessagePump.cs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
45
using System.Text;
56
using System.Threading;
67
using System.Threading.Tasks;
78
using System.Windows.Forms;
89
using System.Windows.Threading;
910

10-
namespace EventHook.Hooks.Helpers
11+
namespace EventHook.Helpers
1112
{
1213
internal class SharedMessagePump
1314
{
@@ -18,36 +19,48 @@ static SharedMessagePump()
1819
{
1920
_scheduler = new Lazy<TaskScheduler>(() =>
2021
{
22+
23+
if(SynchronizationContext.Current!=null)
24+
return TaskScheduler.FromCurrentSynchronizationContext();
25+
2126
TaskScheduler current = null;
22-
//check if current is null, else create a message pump and a shared hwnd handle
27+
28+
//if current task scheduler is null, create a message pump
2329
//http://stackoverflow.com/questions/2443867/message-pump-in-net-windows-service
2430
//use async for performance gain!
25-
var t = new Task(() =>
31+
new Task(() =>
2632
{
27-
2833
System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
2934
{
3035
current = TaskScheduler.FromCurrentSynchronizationContext();
3136
}
3237

3338
), DispatcherPriority.Normal);
3439
System.Windows.Threading.Dispatcher.Run();
35-
});
36-
t.Start();
40+
}).Start();
3741

38-
while (current == null) ;
42+
while (current == null)
43+
{
44+
Thread.Sleep(10);
45+
}
3946

4047
return current;
48+
4149
});
4250

4351
_messageHandler = new Lazy<MessageHandler>(() =>
4452
{
4553
MessageHandler msgHandler = null;
46-
var t = new Task((e) => { msgHandler = new MessageHandler(); }, GetTaskScheduler());
4754

48-
t.Start();
55+
new Task((e) =>
56+
{
57+
msgHandler = new MessageHandler();
58+
}, GetTaskScheduler()).Start();
4959

50-
while (msgHandler == null);
60+
while (msgHandler == null)
61+
{
62+
Thread.Sleep(10);
63+
};
5164

5265
return msgHandler;
5366
});
@@ -60,6 +73,17 @@ internal static TaskScheduler GetTaskScheduler()
6073

6174
internal static IntPtr GetHandle()
6275
{
76+
var handle = IntPtr.Zero;
77+
78+
try
79+
{
80+
handle = Process.GetCurrentProcess().MainWindowHandle;
81+
82+
if (handle != IntPtr.Zero)
83+
return handle;
84+
}
85+
catch { }
86+
6387
return _messageHandler.Value.Handle;
6488
}
6589

EventHook/Hooks/Helpers/WindowHelper.cs renamed to EventHook/Helpers/WindowHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Text;
44
using EventHook.Hooks.Library;
55

6-
namespace EventHook.Hooks.Helpers
6+
namespace EventHook.Helpers
77
{
88
internal class WindowHelper
99
{

EventHook/Hooks/ClipBoardHook.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Windows.Forms;
44
using EventHook.Hooks.Library;
55
using System.Threading.Tasks;
6-
using EventHook.Hooks.Helpers;
6+
using EventHook.Helpers;
77
using System.Threading;
88

99
//https://github.com/MrksKwsnck/Wlipper

EventHook/Hooks/WindowHook.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using EventHook.Hooks.Helpers;
1+
using EventHook.Helpers;
22
using System;
33
using System.Threading;
44
using System.Windows;

EventHook/KeyboardWatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Text;
55
using System.Threading;
66
using EventHook.Hooks;
7-
using EventHook.Hooks.Helpers;
7+
using EventHook.Helpers;
88
using System.Threading.Tasks;
99
using Nito.AsyncEx;
1010

EventHook/MouseWatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using EventHook.Hooks;
2-
using EventHook.Hooks.Helpers;
2+
using EventHook.Helpers;
33
using Nito.AsyncEx;
44
using System;
55
using System.Collections.Generic;

0 commit comments

Comments
 (0)