I've written some WMI code that works really well on most machines. However on some machines (nearly all SSD based macs) this code causes a HUGE performance problem. It causes the WMIPrvSe process to keep hitting I/O Other. If you run another app doing the same watching then it slows to a crawl to the point where the software becomes unusable.
System.Management.WqlEventQuery queryIn;
System.Management.WqlEventQuery queryOut;
System.Management.ManagementScope scope = new System.Management.ManagementScope( "root\\CIMV2" );
scope.Options.EnablePrivileges = true;
try
{
queryIn = new System.Management.WqlEventQuery();
queryIn.EventClassName = "__InstanceCreationEvent";
queryIn.WithinInterval = new TimeSpan( 0, 0, 1 );
//queryIn.GroupWithinInterval = new TimeSpan( 0, 0, 0 );
queryIn.Condition = @"TargetInstance ISA 'Win32_DiskDrive' AND TargetInstance.InterfaceType = 'USB'";
mUSBWatcherIn = new System.Management.ManagementEventWatcher( scope, queryIn );
//adds event handler that’s is fired when the insertion event occurs
mUSBWatcherIn.EventArrived += new System.Management.EventArrivedEventHandler( USBInserted );
queryOut = new System.Management.WqlEventQuery();
queryOut.EventClassName = "__InstanceDeletionEvent";
queryOut.WithinInterval = new TimeSpan( 0, 0, 1 );
//queryOut.GroupWithinInterval = new TimeSpan( 0, 0, 0 );
queryOut.Condition = @"TargetInstance ISA 'Win32_DiskDrive' AND TargetInstance.InterfaceType = 'USB'";
mUSBWatcherOut = new System.Management.ManagementEventWatcher( scope, queryOut );
//adds event handler that’s is fired when the insertion event occurs
mUSBWatcherOut.EventArrived += new System.Management.EventArrivedEventHandler( USBRemoved );
mUSBWatcherIn.Start();//run the watcher
mUSBWatcherOut.Start();
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show( e.Message );
StopUSBWatcher();
}
Does anyone have any idea what could be going on here? If I remove this code then it works perfectly. On other machines, it works perfectly. Its very strange. Any ideas hugely appreciated!