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

Commit fba2001

Browse files
committed
initialize before using message pump
1 parent 1a05669 commit fba2001

File tree

5 files changed

+20
-6
lines changed

5 files changed

+20
-6
lines changed

EventHook/ApplicationWatcher.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public static void Start()
5656
_prevTimeApp = DateTime.Now;
5757

5858
appQueue = new AsyncCollection<object>();
59-
59+
60+
SharedMessagePump.Initialize();
6061
Task.Factory.StartNew(() => { }).ContinueWith(x =>
6162
{
6263
WindowHook.WindowCreated += new GeneralShellHookEventHandler(WindowCreated);

EventHook/ClipboardWatcher.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public static void Start()
4646
{
4747
_clipQueue = new AsyncCollection<object>();
4848

49+
SharedMessagePump.Initialize();
4950
Task.Factory.StartNew(() => { }).ContinueWith(x =>
5051
{
5152
_clip = new ClipBoardHook();

EventHook/Helpers/SharedMessagePump.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ internal class SharedMessagePump
1414
{
1515
static Lazy<TaskScheduler> _scheduler;
1616
static Lazy<MessageHandler> _messageHandler;
17-
17+
1818
static SharedMessagePump()
1919
{
2020
_scheduler = new Lazy<TaskScheduler>(() =>
2121
{
22-
23-
if(SynchronizationContext.Current!=null)
24-
return TaskScheduler.FromCurrentSynchronizationContext();
22+
Dispatcher dispatcher = Dispatcher.FromThread(Thread.CurrentThread);
23+
if (dispatcher != null)
24+
{
25+
if (SynchronizationContext.Current != null)
26+
return TaskScheduler.FromCurrentSynchronizationContext();
27+
}
28+
2529

2630
TaskScheduler current = null;
2731

@@ -66,6 +70,12 @@ static SharedMessagePump()
6670
});
6771
}
6872

73+
internal static void Initialize()
74+
{
75+
GetTaskScheduler();
76+
GetHandle();
77+
}
78+
6979
internal static TaskScheduler GetTaskScheduler()
7080
{
7181
return _scheduler.Value;

EventHook/KeyboardWatcher.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public static void Start()
4848
_kh = new KeyboardHook();
4949
_kh.KeyDown += new RawKeyEventHandler(KListener);
5050
_kh.KeyUp += new RawKeyEventHandler(KListener);
51-
var handle = SharedMessagePump.GetHandle();
51+
52+
SharedMessagePump.Initialize();
5253
Task.Factory.StartNew(() => { }).ContinueWith(x =>
5354
{
5455
_kh.Start();

EventHook/MouseWatcher.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public static void Start()
3636
_mh = new MouseHook();
3737
_mh.MouseAction += MListener;
3838

39+
SharedMessagePump.Initialize();
3940
Task.Factory.StartNew(() => { }).ContinueWith(x =>
4041
{
4142
_mh.Start();

0 commit comments

Comments
 (0)