usbeject icon indicating copy to clipboard operation
usbeject copied to clipboard

Problems when plugged 2 devices at the same time.

Open yaks77 opened this issue 8 years ago • 0 comments

I am developing a tool for my company which analyses every USB device plugged to the system and if it doesn't fulfill some criteria , it is ejected automatically. Doing some search on internet I ended with two little projects which do exactly what I need.

  • For USB plugging detection - http://www.codeproject.com/Articles/18062/Detecting-USB-Drive-Removal-in-a-C-Program
  • For ejecting a USB drive, the code from your site ;)

I just integrated both in a single .NET solution and I simply add an eject command every time I plugged a new USB device.

private void OnDriveArrived(object sender, DriveDetectorEventArgs e)
        {
            // Report the event in the listbox.
            // e.Drive is the drive letter for the device which just arrived, e.g. "E:\\"
            string s = "Drive arrived " + e.Drive;
            listBox1.Items.Add(s);

            VolumeDeviceClass volumes = new VolumeDeviceClass();
            foreach (Volume vol in volumes.Devices) {
                if (vol.LogicalDrive.Equals(e.Drive.Replace("\\","")))
                {
                    listBox1.Items.Add("Attempting to eject drive: " + e.Drive);
                    vol.Eject(false);
                    listBox1.Items.Add("Done ejecting drive.");
                    break;
                }
            }
}

My problem appears when I plug 2 devices at the same time, the code only manages to eject the first device. It seems like that the ejection of the first USB drive also remove the "onArrrived" event of the second drive. As a result, the second drive keeps accessible. Any idea of what might be happening?

I attach the .net solution in case you want to reproduce the issue.

SimpleDetector.zip

Many thanks in advance. Jose

yaks77 avatar Oct 16 '16 15:10 yaks77