diff --git a/JetKarmaBot/JetKarmaBot.cs b/JetKarmaBot/JetKarmaBot.cs index 994f581..dd1f69a 100644 --- a/JetKarmaBot/JetKarmaBot.cs +++ b/JetKarmaBot/JetKarmaBot.cs @@ -59,14 +59,15 @@ namespace JetKarmaBot public async Task Stop() { if (stopped) return; - Client.StopReceiving(); - timeoutWaitTaskToken.Cancel(); + Client?.StopReceiving(); + timeoutWaitTaskToken?.Cancel(); try { - await timeoutWaitTask; + if (timeoutWaitTask != null) + await timeoutWaitTask; } catch (OperationCanceledException) { } - await Timeout.Save(); + await Timeout?.Save(); Dispose(); stopped = true; } @@ -119,8 +120,8 @@ namespace JetKarmaBot public void Dispose() { - timeoutWaitTaskToken.Dispose(); - timeoutWaitTask.Dispose(); + timeoutWaitTaskToken?.Dispose(); + timeoutWaitTask?.Dispose(); } #endregion diff --git a/JetKarmaBot/Program.cs b/JetKarmaBot/Program.cs index 2438505..5831310 100644 --- a/JetKarmaBot/Program.cs +++ b/JetKarmaBot/Program.cs @@ -42,14 +42,13 @@ namespace JetKarmaBot try { - bot.Init().Wait(); + bot.Init().GetAwaiter().GetResult(); log.Info("JetKarmaBot started. Press Ctrl-C to exit..."); - Environment.ExitCode = (int)ExitCode.ErrorRunning; } catch (Exception ex) { log.Error(ex); - Environment.ExitCode = (int)ExitCode.ErrorException; + return (int)ExitCode.ErrorException; } ManualResetEvent quitEvent = new ManualResetEvent(false); try @@ -62,14 +61,14 @@ namespace JetKarmaBot AppDomain.CurrentDomain.ProcessExit += (sender, args) => { log.Info("Received stop request, waiting for exit..."); - bot?.Stop()?.Wait(); + bot?.Stop()?.GetAwaiter().GetResult(); }; } catch { } quitEvent.WaitOne(Timeout.Infinite); log.Info("Waiting for exit..."); - bot?.Stop()?.Wait(); + bot?.Stop()?.GetAwaiter().GetResult(); return (int)ExitCode.Ok; }