-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMsEdgeWrapper.cs
70 lines (58 loc) · 1.89 KB
/
MsEdgeWrapper.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
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
[DllImport("Kernel32")]
public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);
public delegate bool HandlerRoutine(CtrlTypes CtrlType);
private static Process p;
public enum CtrlTypes
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT,
CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT
}
private static bool ConsoleCtrlCheck(CtrlTypes ctrlType)
{
p.Kill();
return true;
}
static Process[] getMsEdgeProcesses()
{
return Process.GetProcessesByName("MicrosoftEdge");
}
static Process startMsEdge()
{
if(getMsEdgeProcesses().Length > 0)
{
throw new Exception("An instance of Microsoft Edge is already running");
}
Process.Start("microsoft-edge:google.com");
Process[] processes = getMsEdgeProcesses();
if(processes.Length == 0)
{
throw new Exception("No instance of Microsoft Edge was available after attempting to start it");
}
if(processes.Length > 1)
{
throw new Exception("More than one instance of Microsoft Edge was available after starting a single instance");
}
return processes[0];
}
static void Main(string[] args)
{
p = startMsEdge();
SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true);
while(true)
{
Thread.Sleep(8000);
}
}
}
}