From 62e91ca61714ca6897ee3f871805c103d2b5dad2 Mon Sep 17 00:00:00 2001 From: bobeaton Date: Mon, 1 Nov 2021 11:57:12 -0500 Subject: [PATCH] #261: Ignore USB GetDrives crash (#272) some users have problems with their USB drive (hardware?) such that GetDrives throws an exception which if not caught, results in a fatal exception for Chorus --- src/Chorus/UI/UsbDriveLocator.cs | 44 ++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/src/Chorus/UI/UsbDriveLocator.cs b/src/Chorus/UI/UsbDriveLocator.cs index 6a5515208..7befe2709 100644 --- a/src/Chorus/UI/UsbDriveLocator.cs +++ b/src/Chorus/UI/UsbDriveLocator.cs @@ -1,10 +1,11 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.ComponentModel; using System.IO; using System.Threading; using System.Windows.Forms; +using SIL.Reporting; namespace Chorus.UI { @@ -58,24 +59,35 @@ public bool CanExtend(object extendee) private void ScanForUsbDrives() { - while (_keepRunning) + // https://github.com/sillsdev/chorus/issues/261 + // some users have problems with their USB drive (hardware?) such that GetDrives throws + // an exception, which if not caught, results in a fatal exception for Chorus + try { - var usbRoots = new SIL.UsbDrive.RetrieveUsbDriveInfo().GetDrives() - .Select(u => u.RootDirectory.FullName); - // In Balsa, the boot device (SD Card or USB Stick) shows up as one of the - // drives and it shouldn't be used for S/R. Balsa will be changed to - // include a .chorus-hidden file in the root paritions that should not be - // used for S/R. A user can include this file in USB attached drive that - // they don't want Chorus to use. - var usbDrives = System.IO.DriveInfo.GetDrives() - .Where(d => usbRoots.Contains(d.RootDirectory.FullName) && - d.RootDirectory.GetFiles(".chorus-hidden").Count() == 0); - lock (_usbDrives) + while (_keepRunning) { - _usbDrives.Clear(); - _usbDrives.AddRange(usbDrives); + var usbRoots = new SIL.UsbDrive.RetrieveUsbDriveInfo().GetDrives() + .Select(u => u.RootDirectory.FullName); + // In Balsa, the boot device (SD Card or USB Stick) shows up as one of the + // drives and it shouldn't be used for S/R. Balsa will be changed to + // include a .chorus-hidden file in the root paritions that should not be + // used for S/R. A user can include this file in USB attached drive that + // they don't want Chorus to use. + var usbDrives = System.IO.DriveInfo.GetDrives() + .Where(d => usbRoots.Contains(d.RootDirectory.FullName) && + d.RootDirectory.GetFiles(".chorus-hidden").Count() == 0); + lock (_usbDrives) + { + _usbDrives.Clear(); + _usbDrives.AddRange(usbDrives); + } + + Thread.Sleep(3000); // check again after 3 second } - Thread.Sleep(3000); // check again after 1 second + } + catch (Exception ex) + { + ErrorReport.ReportNonFatalException(ex); } }