-
Notifications
You must be signed in to change notification settings - Fork 0
/
InfiniteProcessHelper.cs
63 lines (53 loc) · 1.6 KB
/
InfiniteProcessHelper.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
using System;
using System.Diagnostics;
namespace InfiniteProcessLauncher
{
public class InfiniteProcessHelper
{
private string _programName { get; set; }
private string _arguments { get; set; }
private Process _process { get; set; }
public event EventHandler ProcessLaunched;
public InfiniteProcessHelper(string programPath, string arguments = null)
{
_programName = programPath;
_arguments = arguments;
}
public void Run()
{
try
{
KillProcessIfRunning();
CreateProcessAndWait();
}
catch(Exception)
{
KillProcessIfRunning();
}
finally
{
Run();
}
}
//Just extra security but should never happen
private void KillProcessIfRunning()
{
if (_process != null && _process.HasExited == false)
{
_process.Kill();
_process = null;
}
}
private void CreateProcessAndWait()
{
_process = new Process();
_process.StartInfo.FileName = _programName;
_process.StartInfo.Arguments = _arguments;
//No need to subscribe to the event. The WaitForExit is going to end and the Run() is alled again in the Finally
//_process.Exited += _process_Exited;
_process.Start();
ProcessLaunched(this, new EventArgs());
_process.WaitForExit();
}
}
}