private static void Shutdown() { if ((systemEvents != null) && (systemEvents.windowHandle != IntPtr.Zero)) { lock (procLockObject) { if (systemEvents != null) { startupRecreates = true; if (windowThread != null) { eventThreadTerminated = new ManualResetEvent(false); Microsoft.Win32.UnsafeNativeMethods.PostMessage(new HandleRef(systemEvents, systemEvents.windowHandle), 0x12, IntPtr.Zero, IntPtr.Zero); eventThreadTerminated.WaitOne(); windowThread.Join(); } else { systemEvents.Dispose(); systemEvents = null; } } } } }
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; } } } }
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 Shutdown() { if (systemEvents != null && systemEvents.windowHandle != IntPtr.Zero) { lock(procLockObject) { if (systemEvents != null) { startupRecreates = true; // If we are using system events from another thread, request that it terminate // if (windowThread != null) { eventThreadTerminated = new ManualResetEvent(false); #if DEBUG int pid; int thread = SafeNativeMethods.GetWindowThreadProcessId(new HandleRef(systemEvents, systemEvents.windowHandle), out pid); Debug.Assert(thread != SafeNativeMethods.GetCurrentThreadId(), "Don't call Shutdown on the system events thread"); #endif UnsafeNativeMethods.PostMessage(new HandleRef(systemEvents, systemEvents.windowHandle), NativeMethods.WM_QUIT, IntPtr.Zero, IntPtr.Zero); eventThreadTerminated.WaitOne(); windowThread.Join(); //avoids an AppDomainUnloaded exception on our background thread. } else { systemEvents.Dispose(); systemEvents = null; } } } } }