///<summary> /// Creates an instance of the <see cref="WindowsDeviceNotifier"/> class. /// See the <see cref="IDeviceNotifier"/> interface or <see cref="DeviceNotifier.OpenDeviceNotifier"/> method for more information ///</summary> ///<remarks> ///To make your code platform-independent use the <see cref="DeviceNotifier.OpenDeviceNotifier"/> method for creating instances. ///</remarks> public WindowsDeviceNotifier() { // We can only hear WM_DEVICECHANGE messages if we're an STA thread that's properly pumping windows messages. // There's no easy way to tell if the calling thread is pumping messages, so just check the apartment state, and assume people // aren't creating STA threads without a message pump. if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA) { mNotifyWindow = new DevNotifyNativeWindow(OnHandleChange, OnDeviceChange); } else { // We're definitely not in an STA Thread (we're probably running in a console), so start a new STA Thread, and call // Application.Run() to start pumping windows messages. Thread staThread = new Thread(new ThreadStart(() => { mNotifyWindow = new DevNotifyNativeWindow(OnHandleChange, OnDeviceChange); Application.Run(); })); staThread.SetApartmentState(ApartmentState.STA); staThread.Name = "DevNotifyNativeWindow STA Thread"; staThread.Start(); } }
///<summary> /// Creates an instance of the <see cref="WindowsDeviceNotifier"/> class. /// See the <see cref="IDeviceNotifier"/> interface or <see cref="DeviceNotifier.OpenDeviceNotifier"/> method for more information ///</summary> ///<remarks> ///To make your code platform-independent use the <see cref="DeviceNotifier.OpenDeviceNotifier"/> method for creating instances. ///</remarks> public WindowsDeviceNotifier() { mNotifyWindow = new DevNotifyNativeWindow(OnHandleChange, OnDeviceChange); }