private void DriveMonitorThread() { while (true) { var invalidDrive = Path.GetPathRoot(Application.StartupPath); var allDrives = DriveInfo.GetDrives().Where(d => d.IsReady && d.DriveType.Equals(DriveType.Removable) && !d.Name.Equals(invalidDrive, StringComparison.InvariantCultureIgnoreCase)); var formatting = ThreadSafeWinControlHelper.GetDriveInfoProgress(flowLayoutPanel1, DriveInfoProgressStatus.Formatting).Select(c => c.Volume); var addDrives = allDrives.Where(d => !formatting.Contains(d.Name)).Except(_drives, DriveInfoComparer); foreach (var drive in addDrives) { _drives.Add(drive); } Thread.Sleep(300); } }
private void DetectDriveDisconnectThread(DriveInfo drive) { var control = ThreadSafeWinControlHelper.GetDriveInfoProgress(flowLayoutPanel1, drive.Name); while (drive.IsReady || control.Status == DriveInfoProgressStatus.Formatting) { if (control == null) { control = ThreadSafeWinControlHelper.GetDriveInfoProgress(flowLayoutPanel1, drive.Name); } // Waiting for disconnect. Thread.Sleep(300); } lock (_drives) { _drives.Remove(drive); } }