This repository was archived by the owner on Apr 21, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +14
-7
lines changed Expand file tree Collapse file tree 1 file changed +14
-7
lines changed Original file line number Diff line number Diff line change @@ -12,9 +12,11 @@ namespace EventHook.Helpers
12
12
{
13
13
internal class SharedMessagePump
14
14
{
15
+ private static bool _hasUIThread = false ;
16
+
15
17
static Lazy < TaskScheduler > _scheduler ;
16
18
static Lazy < MessageHandler > _messageHandler ;
17
-
19
+
18
20
static SharedMessagePump ( )
19
21
{
20
22
_scheduler = new Lazy < TaskScheduler > ( ( ) =>
@@ -23,10 +25,12 @@ static SharedMessagePump()
23
25
if ( dispatcher != null )
24
26
{
25
27
if ( SynchronizationContext . Current != null )
28
+ {
29
+ _hasUIThread = true ;
26
30
return TaskScheduler . FromCurrentSynchronizationContext ( ) ;
31
+ }
27
32
}
28
33
29
-
30
34
TaskScheduler current = null ;
31
35
32
36
//if current task scheduler is null, create a message pump
@@ -85,14 +89,17 @@ internal static IntPtr GetHandle()
85
89
{
86
90
var handle = IntPtr . Zero ;
87
91
88
- try
92
+ if ( _hasUIThread )
89
93
{
90
- handle = Process . GetCurrentProcess ( ) . MainWindowHandle ;
94
+ try
95
+ {
96
+ handle = Process . GetCurrentProcess ( ) . MainWindowHandle ;
91
97
92
- if ( handle != IntPtr . Zero )
93
- return handle ;
98
+ if ( handle != IntPtr . Zero )
99
+ return handle ;
100
+ }
101
+ catch { }
94
102
}
95
- catch { }
96
103
97
104
return _messageHandler . Value . Handle ;
98
105
}
You can’t perform that action at this time.
0 commit comments