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
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 }
You can’t perform that action at this time.
0 commit comments