public static void StopTimer() { log.InfoFormat("Stopping Trove game status checking"); try { _UpdateTroveGameStatusTimer?.Stop(); TrovesaurusApi.UpdateTroveGameStatus(false); } catch (Exception ex) { log.Error("Error stopping Trove game status detection", ex); } }
private static void _UpdateTroveGameStatusTimer_Elapsed(object sender, ElapsedEventArgs e) { try { bool isTroveRunning = false; foreach (var process in Process.GetProcessesByName(Path.GetFileNameWithoutExtension(TroveLocation.TroveExecutableFileName))) { try { string cmdLine = process.GetCommandLine(); if (!cmdLine.Contains("tool")) { isTroveRunning = true; break; } } // Catch and ignore "access denied" exceptions. catch (Win32Exception ex) when(ex.HResult == -2147467259) { } // Catch and ignore "Cannot process request because the process (<pid>) has exited." exceptions. catch (InvalidOperationException ex) when(ex.HResult == -2146233079) { } } if (isTroveRunning) { if (!_Online.HasValue || _Online.Value == false) { _Online = true; string status = TrovesaurusApi.UpdateTroveGameStatus(_Online.Value); log.InfoFormat("Trove game detected running, updated Trovesaurus game status (return value: {0})", status); } } else { if (!_Online.HasValue || _Online.Value == true) { _Online = false; string status = TrovesaurusApi.UpdateTroveGameStatus(_Online.Value); log.InfoFormat("Trove game detected not running, updated Trovesaurus game status (return value: {0})", status); } } } catch (Exception ex) { log.Error("Error in Trove game status detection", ex); } }