private static void SDMountThread() { const int POLL_TIME = 500; // check every 500 millisecond bool sdExists; while (true) { try // If SD card was removed while mounting, it may throw exceptions { sdExists = sdDetectPin.Read() == false; // make sure it is fully inserted and stable if (sdExists) { Thread.Sleep(50); sdExists = sdDetectPin.Read() == false; } if (sdExists && storage == null) { SetupStorage(); } else if (!sdExists && storage != null) { Trace.CloseTrace(); storage.UnmountFileSystem(); storage.Dispose(); storage = null; } } catch { if (storage != null) { storage.Dispose(); storage = null; } } Thread.Sleep(POLL_TIME); } }