-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppManager.cs
More file actions
53 lines (43 loc) · 1.4 KB
/
Copy pathAppManager.cs
File metadata and controls
53 lines (43 loc) · 1.4 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
50
51
52
53
namespace Hint;
using System.Diagnostics;
public class AppManager
{
private readonly List<AppDefinition> apps;
private readonly Dictionary<string, Process?> running = new();
public AppManager(List<AppDefinition> apps)
{
this.apps = apps;
foreach (var a in apps) running[a.Name] = null;
}
public bool TryGetApp(string name, out AppDefinition app)
{
app = apps.FirstOrDefault(a => a.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
return app != null;
}
public async Task EnsureAllRunningAsync()
{
foreach (var a in apps) await EnsureRunning(a);
}
public async Task EnsureRunning(AppDefinition a)
{
if (running[a.Name] != null && !running[a.Name].HasExited) return;
var parts = SplitCommand(a.StartCommand);
var psi = new ProcessStartInfo(parts.command, parts.args)
{
WorkingDirectory = a.Path,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false
};
var p = Process.Start(psi);
running[a.Name] = p;
await Task.Delay(500);
}
private (string command, string args) SplitCommand(string cmd)
{
if (string.IsNullOrEmpty(cmd)) return ("", "");
var i = cmd.IndexOf(' ');
if (i < 0) return (cmd, "");
return (cmd[..i], cmd[(i + 1)..]);
}
}