How to receive Plug & Play device notifications without a windows form

前端 未结 3 1888
星月不相逢
星月不相逢 2020-11-27 14:35

I am trying to write a class library that can catch the windows messages to notify me if a device has been attached or removed. Normally, in a windows forms app I would just

3条回答
  •  星月不相逢
    2020-11-27 15:20

    You'll need a window, there's no way around that. Here's a sample implementation. Implement an event handler for the DeviceChangeNotifier.DeviceNotify event to get notifications. Call the DeviceChangeNotifier.Start() method at the start of your program. Call DeviceChangeNotifier.Stop() at the end of your program. Beware that the DeviceNotify event is raised on a background thread, be sure to lock as needed to keep your code thread-safe.

    using System;
    using System.Windows.Forms;
    using System.Threading;
    
    class DeviceChangeNotifier : Form {
      public delegate void DeviceNotifyDelegate(Message msg);
      public static event DeviceNotifyDelegate DeviceNotify;
      private static DeviceChangeNotifier mInstance;
    
      public static void Start() {
        Thread t = new Thread(runForm);
        t.SetApartmentState(ApartmentState.STA);
        t.IsBackground = true;
        t.Start();
      }
      public static void Stop() {
        if (mInstance == null) throw new InvalidOperationException("Notifier not started");
        DeviceNotify = null;
        mInstance.Invoke(new MethodInvoker(mInstance.endForm));
      }
      private static void runForm() {
        Application.Run(new DeviceChangeNotifier());
      }
    
      private void endForm() {
        this.Close();
      }
      protected override void SetVisibleCore(bool value) {
        // Prevent window getting visible
        if (mInstance == null) CreateHandle();
        mInstance = this;
        value = false;
        base.SetVisibleCore(value);
      }
      protected override void WndProc(ref Message m) {
        // Trap WM_DEVICECHANGE
        if (m.Msg == 0x219) {
          DeviceNotifyDelegate handler = DeviceNotify;
          if (handler != null) handler(m);
        }
        base.WndProc(ref m);
      }
    }
    

提交回复
热议问题