private void DoStateChanged(UsbStateChangedEventArgs e) { if (admin.lockdownoverride) { invalidcounter = 0; SendNotificationFromFirebaseCloud("Lockdown Manual Override by Admin"); } if (admin.whitelisting) { Thread threadx = new Thread(() => { WebClient client = new WebClient(); string hash = sha256(getUSBSerial(e.Disk.Name)); string url = "http://cse323.ddns.net/cse323/addwhitelist.php?P=12345&hash=" + hash; client.DownloadData(url); url = "http://cse323.ddns.net/cse323/addlog.php?P=12345&log=NewWhitelisting"; client.DownloadData(url); MessageBox.Show("USB Drive added to the whitelist!"); SendNotificationFromFirebaseCloud("New USB Drive added to whitelist"); }); threadx.Start(); threadx.Join(); //wait for the thread to finish } admin.whitelisting = false; if (admin.stopMonitor) { return; } textBox.AppendText(e.State + " " + e.Disk.ToString() + CR); Thread thread = new Thread(() => { string hash = sha256(getUSBSerial(e.Disk.Name)); if (!checkAgainstWhitelist(hash)) { usb.Eject(e.Disk.Name); } }); thread.Start(); thread.Join(); //wait for the thread to finish }
/// <summary> /// Internally handle state changes and notify listeners. /// </summary> /// <param name="e"></param> private void DoStateChanged(UsbStateChangedEventArgs e) { if (handler != null) { UsbDisk disk = e.Disk; // we can only interrogate drives that are added... // cannot see something that is no longer there! if ((e.State == UsbStateChange.Added) && (e.Disk.Name[0] != '?')) { // the following Begin/End invokes looks strange but are required // to resolve a "DisconnectedContext was detected" exception which // occurs when the current thread terminates before the WMI queries // can complete. I'm not exactly sure why that would happen... GetDiskInformationDelegate gdi = new GetDiskInformationDelegate(GetDiskInformation); IAsyncResult result = gdi.BeginInvoke(e.Disk, null, null); gdi.EndInvoke(result); } handler(e); } }