private static void EnsureSystemEvents(bool requireHandle, bool throwOnRefusal)
 {
     if (systemEvents == null)
     {
         lock (procLockObject)
         {
             if (systemEvents == null)
             {
                 if (Thread.GetDomain().GetData(".appDomain") != null)
                 {
                     if (throwOnRefusal)
                     {
                         throw new InvalidOperationException(SR.GetString("ErrorSystemEventsNotSupported"));
                     }
                 }
                 else
                 {
                     if (!UserInteractive || (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA))
                     {
                         systemEvents = new SystemEvents();
                         systemEvents.Initialize();
                     }
                     else
                     {
                         eventWindowReady = new ManualResetEvent(false);
                         systemEvents = new SystemEvents();
                         windowThread = new Thread(new ThreadStart(systemEvents.WindowThreadProc));
                         windowThread.IsBackground = true;
                         windowThread.Name = ".NET SystemEvents";
                         windowThread.Start();
                         eventWindowReady.WaitOne();
                     }
                     if (requireHandle && (systemEvents.windowHandle == IntPtr.Zero))
                     {
                         throw new ExternalException(SR.GetString("ErrorCreateSystemEvents"));
                     }
                     startupRecreates = false;
                 }
             }
         }
     }
 }
        private static void EnsureSystemEvents(bool requireHandle, bool throwOnRefusal) {

            // The secondary check here is to detect asp.net.  Asp.net uses multiple
            // app domains to field requests and we do not want to gobble up an 
            // additional thread per domain.  So under this scenario SystemEvents
            // becomes a nop.
            //
            if (systemEvents == null) {
                lock (procLockObject) {
                    if (systemEvents == null) {
                        if (Thread.GetDomain().GetData(".appDomain") != null) {
                            if (throwOnRefusal) {
                                throw new InvalidOperationException(SR.GetString(SR.ErrorSystemEventsNotSupported));
                            }
                            return;
                        }

                        // If we are creating system events on a thread declared as STA, then
                        // just share the thread.
                        //
                        if (!UserInteractive || Thread.CurrentThread.GetApartmentState() == ApartmentState.STA) {
                            systemEvents = new SystemEvents();
                            systemEvents.Initialize();
                        }
                        else {
                            eventWindowReady = new ManualResetEvent(false);
                            systemEvents = new SystemEvents();
                            windowThread = new Thread(new ThreadStart(systemEvents.WindowThreadProc));
                            windowThread.IsBackground = true;
                            windowThread.Name = ".NET SystemEvents";
                            windowThread.Start();
                            eventWindowReady.WaitOne();
                        }

                        if (requireHandle && systemEvents.windowHandle == IntPtr.Zero) {
                            // In theory, it's not the end of the world that
                            // we don't get system events.  Unfortunately, the main reason windowHandle == 0
                            // is CreateWindowEx failed for mysterious reasons, and when that happens,
                            // subsequent (and more important) CreateWindowEx calls also fail.
                            // See ASURT #44424 for a rather lengthy discussion of this.
                            throw new ExternalException(SR.GetString(SR.ErrorCreateSystemEvents));
                        }

                        startupRecreates = false;
                    }
                }
            }
        }