title: WMI USB Monitor Multiple Events Fired
body: <p>I've been trying to monitor when USB devices are inserted or removed and it seems to be working pretty well. The only thing that troubling me now is that the event is fired multiple times each time I plug a device in or remove it.</p>
  
  <p>I can group the events with no problem but I am curious why it is happening in the first place.</p>
  
  <p>This is the query I'm using</p>
  
  <pre><code>SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2 or EventType = 3
  </code></pre>
  
  <p>Which fires when a device is inserted or removed. And the following modified version...</p>
  
  <pre><code>SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2 or EventType = 3 GROUP WITHIN 1
  </code></pre>
  
  <p>Groups the events over a 1 second interval. Can someone explain why the events are triggered multiple times?</p>
  
  <p>For completeness here is the rest of the code.</p>
  
  <pre><code>        static void Main(string[] args)
      {
          var watcher = new ManagementEventWatcher();
          var query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2 or EventType = 3 GROUP WITHIN 1 ");
          watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
          watcher.Query = query;
          watcher.Start();
  
          Console.WriteLine("Press a key to exit.");
          Console.ReadKey();
      }
  
      static void watcher_EventArrived(object sender, EventArrivedEventArgs e)
      {
          Console.WriteLine(string.Format("--&gt; {0}", e.NewEvent.GetType().Name));
          Console.WriteLine(string.Format("   {0}", e.NewEvent.ClassPath.ClassName));
          Console.WriteLine(string.Format("   Properties [{0}]", e.NewEvent.Properties.Count));
          foreach (var prop in e.NewEvent.Properties)
          {
              Console.WriteLine(string.Format("     Name: {0} Origin: {1} Type: {2} = {3}", prop.Name, prop.Origin, prop.Type.ToString(),prop.Value==null?"{null}":prop.Value.ToString()));
          }
  }
  </code></pre>