-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
94 lines (81 loc) · 2.78 KB
/
Program.cs
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System.Diagnostics;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.UI.WindowsAndMessaging;
namespace GameAwaiter
{
public static class Program
{
public static int Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("Path not set");
if (OperatingSystem.IsWindowsVersionAtLeast(10, 0, 10240, 0))
PInvoke.MessageBox(new HWND(0), "Path not set", "GameAwaiter", MESSAGEBOX_STYLE.MB_OK);
else
{
Console.WriteLine("Path not set");
Console.ReadKey();
}
return -1;
}
var app = args[0];
if (!File.Exists(app))
{
if (OperatingSystem.IsWindowsVersionAtLeast(10, 0, 10240, 0))
PInvoke.MessageBox(new HWND(0), $"Executable not found at \"{app}\"", "GameAwaiter", MESSAGEBOX_STYLE.MB_OK);
else
{
Console.WriteLine($"Executable not found at \"{app}\"");
Console.ReadKey();
}
return -1;
}
var delay = args.Length >= 2 ? int.Parse(args[1]) : 5000;
if (WaitForNewProcess(app))
return 0;
Console.WriteLine($"Starting: {app}");
Thread.Sleep(delay);
using (var p = new Process())
{
p.StartInfo.Verb = "runas";
p.StartInfo.FileName = app;
p.StartInfo.WorkingDirectory = Path.GetDirectoryName(app);
p.StartInfo.UseShellExecute = true;
p.Start();
Console.WriteLine($"PID: {p.Id}");
p.WaitForExit();
}
WaitForNewProcess(app);
return 0;
}
private static bool WaitForNewProcess(string app)
{
using (var process1 = GetNewProcess(app))
if (process1 == null)
return false;
while (true)
{
using (var process2 = GetNewProcess(app))
{
if (process2 == null)
return true;
Console.WriteLine($"PID: {process2.Id}");
process2.WaitForExit();
}
}
}
private static Process? GetNewProcess(string app)
{
var processes = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(app));
if (processes.Length != 1)
{
foreach (var p in processes)
p.Dispose();
return null;
}
return processes[0];
}
}
}