static NANDService() { Validator = DefaultValidator; // Create event handlers to detect when a device is added or removed CreateWatcher = new ManagementEventWatcher(); WqlEventQuery createQuery = new WqlEventQuery("SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_DiskDrive'"); CreateWatcher.EventArrived += new EventArrivedEventHandler((s, e) => { if (NAND == null) // ignore if we already found the Switch { Refresh(); } }); CreateWatcher.Query = createQuery; DeleteWatcher = new ManagementEventWatcher(); WqlEventQuery deleteQuery = new WqlEventQuery("SELECT * FROM __InstanceDeletionEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_DiskDrive'"); DeleteWatcher.EventArrived += new EventArrivedEventHandler((s, e) => { if (NAND != null) // ignore if we haven't found a Switch yet { if (CurrentDisk != null) { // means the NAND is access over USB, so we need to determine if it bool found = false; foreach (DiskInfo info in CreateDiskInfos(GetDisks())) // search to see if we can match the DiskInfo with an existing device { if (info.PhysicalName.Equals(CurrentDisk.PhysicalName)) { found = true; break; } } if (!found) { OnNANDRemoved?.Invoke(); } } } }); DeleteWatcher.Query = deleteQuery; }
public static void ResetHandlers() { // Clear event handlers if (OnNANDPluggedIn != null) { foreach (Delegate d in OnNANDPluggedIn.GetInvocationList()) { OnNANDPluggedIn -= (NANDChangedEventHandler)d; } } if (OnNANDRemoved != null) { foreach (Delegate d in OnNANDRemoved.GetInvocationList()) { OnNANDRemoved -= (NANDChangedEventHandler)d; } } // Reset validator to default Validator = DefaultValidator; }