diff --git a/ArchiSteamFarm/ArchiServiceInstaller.cs b/ArchiSteamFarm/ArchiServiceInstaller.cs index 475a86dc3..195503d9a 100644 --- a/ArchiSteamFarm/ArchiServiceInstaller.cs +++ b/ArchiSteamFarm/ArchiServiceInstaller.cs @@ -12,7 +12,8 @@ namespace ArchiSteamFarm { ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller(); serviceInstaller.ServiceName = SharedInfo.ServiceName; - serviceInstaller.DisplayName = SharedInfo.ServiceDescription; + serviceInstaller.DisplayName = SharedInfo.ServiceName; + serviceInstaller.Description = SharedInfo.ServiceDescription; // Defaulting to only starting when a user starts it, can be easily changed after install serviceInstaller.StartType = ServiceStartMode.Manual; diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 76c909309..8fc35d352 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -332,6 +332,17 @@ namespace ArchiSteamFarm { return false; } + internal void Stop() { + Logging.LogGenericInfo("Stopping...", BotName); + KeepRunning = false; + + if (SteamClient.IsConnected) { + SteamClient.Disconnect(); + } + + Program.OnBotShutdown(); + } + internal void OnFarmingStopped() => ResetGamesPlayed(); internal async Task OnFarmingFinished(bool farmedSomething) { @@ -478,17 +489,6 @@ namespace ArchiSteamFarm { SteamClient.Connect(); } - private void Stop() { - Logging.LogGenericInfo("Stopping...", BotName); - KeepRunning = false; - - if (SteamClient.IsConnected) { - SteamClient.Disconnect(); - } - - Program.OnBotShutdown(); - } - private bool IsMaster(ulong steamID) { if (steamID != 0) { return (steamID == BotConfig.SteamMasterID) || IsOwner(steamID); diff --git a/ArchiSteamFarm/Program.cs b/ArchiSteamFarm/Program.cs index 58eb534b9..ec91f4c0e 100644 --- a/ArchiSteamFarm/Program.cs +++ b/ArchiSteamFarm/Program.cs @@ -258,7 +258,7 @@ namespace ArchiSteamFarm { } internal static void Exit(int exitCode = 0) { - WCF.StopServer(); + Shutdown(); Environment.Exit(exitCode); } @@ -335,6 +335,10 @@ namespace ArchiSteamFarm { } internal static void OnBotShutdown() { + if (ShutdownResetEvent.IsSet) { + return; + } + if (Bot.Bots.Values.Any(bot => bot.KeepRunning)) { return; } @@ -348,6 +352,15 @@ namespace ArchiSteamFarm { ShutdownResetEvent.Set(); } + private static void Shutdown() { + WCF.StopServer(); + ShutdownResetEvent.Set(); + + foreach (Bot bot in Bot.Bots.Values) { + bot.Stop(); + } + } + private static void InitServices() { GlobalConfig = GlobalConfig.Load(Path.Combine(ConfigDirectory, GlobalConfigFile)); if (GlobalConfig == null) { @@ -518,12 +531,7 @@ namespace ArchiSteamFarm { } private static void Main(string[] args) { - if (!Environment.UserInteractive) { - // Service - using (Service service = new Service()) { - ServiceBase.Run(service); - } - } else { + if (Environment.UserInteractive) { // App Init(args); @@ -532,22 +540,26 @@ namespace ArchiSteamFarm { // We got a signal to shutdown Exit(); + } else { + // Service + using (Service service = new Service()) { + ServiceBase.Run(service); + } } } - private sealed class Service : ServiceBase { internal Service() { ServiceName = SharedInfo.ServiceName; } - protected override void OnStart(string[] args) => new Thread(() => { + protected override void OnStart(string[] args) => Task.Run(() => { Init(args); ShutdownResetEvent.Wait(); Stop(); - }).Start(); + }); - protected override void OnStop() => ShutdownResetEvent.Set(); + protected override void OnStop() => Shutdown(); } }