Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion Torch.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Management;
using System.Net;
using System.Reflection;
using System.ServiceProcess;
Expand Down Expand Up @@ -62,7 +63,7 @@ public static void Main(string[] args)
}

// Breaks on Windows Server 2019
if ((!new ComputerInfo().OSFullName.Contains("Server 2019") && !new ComputerInfo().OSFullName.Contains("Server 2022")) && !Environment.UserInteractive)
if (IsRunningAsService())
{
using (var service = new TorchService(args))
ServiceBase.Run(service);
Expand All @@ -81,5 +82,20 @@ public static void Main(string[] args)
return;
}
}

static bool IsRunningAsService()
{
// Check if the parent process is services.exe
using(var currentProcess = Process.GetCurrentProcess())
using(var parentProcess = Process.GetProcessById(GetParentProcessId(currentProcess.Id)))
return parentProcess != null && parentProcess.ProcessName.StartsWith("services", StringComparison.OrdinalIgnoreCase);
}

static int GetParentProcessId(int processId)
{
using(var searcher = new ManagementObjectSearcher($"SELECT ParentProcessId FROM Win32_Process WHERE ProcessId={processId}"))
using(var results = searcher.Get())
return Convert.ToInt32(results.OfType<ManagementObject>().First()["ParentProcessId"]);
}
}
}
3 changes: 2 additions & 1 deletion Torch.Server/Torch.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
<Reference Include="System.Drawing" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Management" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Security.AccessControl, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.AccessControl.4.4.0\lib\net461\System.Security.AccessControl.dll</HintPath>
Expand Down Expand Up @@ -538,4 +539,4 @@
<PropertyGroup>
<PostBuildEvent>copy "$(SolutionDir)NLog.config" "$(TargetDir)" &amp; copy "$(SolutionDir)NLog-user.config" "$(TargetDir)"</PostBuildEvent>
</PropertyGroup>
</Project>
</Project>