-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessRestarter.cs
More file actions
49 lines (45 loc) · 1.56 KB
/
Copy pathProcessRestarter.cs
File metadata and controls
49 lines (45 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
namespace GitHubAppSync
{
/// <summary>
/// Restarts the running application after an update has been applied.
/// </summary>
internal static class ProcessRestarter
{
internal static void Restart()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
var executable = CurrentExecutablePath();
if (executable != null && File.Exists(executable))
{
Process.Start(new ProcessStartInfo(executable) { UseShellExecute = true });
return;
}
// Without a path to relaunch there is nothing better to do than exit; the
// supervisor or the user brings the application back on the new build.
Environment.Exit(0);
return;
}
// On Linux and macOS the application normally runs under a supervisor (systemd unit or
// launchd daemon) that restarts it. Exiting cleanly hands control back to it; spawning a
// child from a daemon would detach it from that supervision.
Environment.Exit(0);
}
private static string CurrentExecutablePath()
{
try
{
using var process = Process.GetCurrentProcess();
return process.MainModule?.FileName;
}
catch
{
return null;
}
}
}
}