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

Commit 22edc83

Browse files
committed
use flag to identify UI thread
1 parent fba2001 commit 22edc83

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

EventHook/Helpers/SharedMessagePump.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ namespace EventHook.Helpers
1212
{
1313
internal class SharedMessagePump
1414
{
15+
private static bool _hasUIThread = false;
16+
1517
static Lazy<TaskScheduler> _scheduler;
1618
static Lazy<MessageHandler> _messageHandler;
17-
19+
1820
static SharedMessagePump()
1921
{
2022
_scheduler = new Lazy<TaskScheduler>(() =>
@@ -23,10 +25,12 @@ static SharedMessagePump()
2325
if (dispatcher != null)
2426
{
2527
if (SynchronizationContext.Current != null)
28+
{
29+
_hasUIThread = true;
2630
return TaskScheduler.FromCurrentSynchronizationContext();
31+
}
2732
}
2833

29-
3034
TaskScheduler current = null;
3135

3236
//if current task scheduler is null, create a message pump
@@ -85,14 +89,17 @@ internal static IntPtr GetHandle()
8589
{
8690
var handle = IntPtr.Zero;
8791

88-
try
92+
if (_hasUIThread)
8993
{
90-
handle = Process.GetCurrentProcess().MainWindowHandle;
94+
try
95+
{
96+
handle = Process.GetCurrentProcess().MainWindowHandle;
9197

92-
if (handle != IntPtr.Zero)
93-
return handle;
98+
if (handle != IntPtr.Zero)
99+
return handle;
100+
}
101+
catch { }
94102
}
95-
catch { }
96103

97104
return _messageHandler.Value.Handle;
98105
}

0 commit comments

Comments
 (0)