Improve ArchiService

- Correct DisplayName
- Implement proper Shutdown sequence instead of 20 seconds timeout-kill
- Misc
This commit is contained in:
JustArchi
2016-06-28 07:14:51 +02:00
parent 5a8701444a
commit 7351d07518
3 changed files with 36 additions and 23 deletions

View File

@@ -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();
}
}