adding ability to run as a windows service

This commit is contained in:
Justin Gerhardt
2016-06-27 23:39:47 -04:00
parent a4383cdb89
commit 056b793262
4 changed files with 90 additions and 8 deletions

View File

@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
namespace ArchiSteamFarm
{
[RunInstaller(true)]
public sealed class ArchiServiceInstaller : System.Configuration.Install.Installer
{
public ArchiServiceInstaller()
{
ServiceInstaller serviceInstaller = new ServiceInstaller();
ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();
serviceInstaller.ServiceName = SharedInfo.ServiceName;
serviceInstaller.DisplayName = SharedInfo.ServiceDescription;
//defaulting to only starting when a user starts it, can be easily changed after install
serviceInstaller.StartType = ServiceStartMode.Manual;
//system account, requires admin privilege to install
serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
Installers.Add(serviceInstaller);
Installers.Add(serviceProcessInstaller);
}
}
}

View File

@@ -88,16 +88,22 @@
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration.Install" />
<Reference Include="System.Core" />
<Reference Include="System.Security" />
<Reference Include="System.Management" />
<Reference Include="System.ServiceModel" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Net.Http" />
<Reference Include="System.ServiceProcess" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ArchiHandler.cs" />
<Compile Include="ArchiWebHandler.cs" />
<Compile Include="ArchiServiceInstaller.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Bot.cs" />
<Compile Include="BotConfig.cs" />
<Compile Include="ConcurrentEnumerator.cs" />
@@ -123,7 +129,9 @@
<Compile Include="WebBrowser.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="App.config">
<SubType>Designer</SubType>
</None>
<None Include="config\ASF.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

View File

@@ -30,6 +30,7 @@ using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
using System.Threading;
using System.Threading.Tasks;
using ArchiSteamFarm.JSON;
@@ -276,7 +277,7 @@ namespace ArchiSteamFarm {
return null;
}
if (GlobalConfig.Headless) {
if (GlobalConfig.Headless || !Environment.UserInteractive) {
Logging.LogGenericWarning("Received a request for user input, but process is running in headless mode!");
return null;
}
@@ -517,13 +518,52 @@ namespace ArchiSteamFarm {
}
private static void Main(string[] args) {
Init(args);
if (!Environment.UserInteractive)
{
//Running as service
using (var service = new Service())
ServiceBase.Run(service);
}
else
{
//Console app
Init(args);
// Wait for signal to shutdown
ShutdownResetEvent.Wait();
// Wait for signal to shutdown
ShutdownResetEvent.Wait();
// We got a signal to shutdown
Exit();
// We got a signal to shutdown
Exit();
}
}
}
sealed class Service : ServiceBase
{
public Service()
{
ServiceName = SharedInfo.ServiceName;
}
protected override void OnStart(string[] args)
{
//Services must not block, they have 30 seconds to return from the start method or are killed
new Thread(() =>
{
Init(args);
ShutdownResetEvent.Wait();
Stop();
}).Start();
}
protected override void OnStop()
{
ShutdownResetEvent.Set();
}
}
}
}

View File

@@ -28,5 +28,7 @@ namespace ArchiSteamFarm {
internal const string Version = "2.1.1.3";
internal const string GithubRepo = "JustArchi/ArchiSteamFarm";
internal const string ServiceName = "ArchiSteamFarm";
internal const string ServiceDescription = "ASF is an application that allows you to farm steam cards using multiple steam accounts simultaneously.";
}
}