-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProgram.cs
100 lines (87 loc) · 3.04 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
95
96
97
98
99
100
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
using Microsoft.Windows.AppLifecycle;
using WinRT;
namespace Coder.Desktop.App;
#if DISABLE_XAML_GENERATED_MAIN
public static class Program
{
private static App? app;
#if DEBUG
[DllImport("kernel32.dll")]
private static extern bool AllocConsole();
#endif
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern int MessageBoxW(IntPtr hWnd, string text, string caption, int type);
[STAThread]
private static void Main(string[] args)
{
try
{
ComWrappersSupport.InitializeComWrappers();
AppInstance mainInstance = GetMainInstance();
if (!mainInstance.IsCurrent)
{
var activationArgs = AppInstance.GetCurrent().GetActivatedEventArgs();
mainInstance.RedirectActivationToAsync(activationArgs).AsTask().Wait();
return;
}
// Register for URI handling (known as "protocol activation")
#if DEBUG
const string scheme = "coder-debug";
#else
const string scheme = "coder";
#endif
var thisBin = Assembly.GetExecutingAssembly().Location;
ActivationRegistrationManager.RegisterForProtocolActivation(scheme, thisBin + ",1", "Coder Desktop", "");
Application.Start(p =>
{
var context = new DispatcherQueueSynchronizationContext(DispatcherQueue.GetForCurrentThread());
SynchronizationContext.SetSynchronizationContext(context);
app = new App();
app.UnhandledException += (_, e) =>
{
e.Handled = true;
ShowExceptionAndCrash(e.Exception);
};
// redirections via RedirectActivationToAsync above get routed to the App
mainInstance.Activated += app.OnActivated;
});
}
catch (Exception e)
{
ShowExceptionAndCrash(e);
}
}
private static AppInstance GetMainInstance()
{
#if !DEBUG
const string appInstanceName = "Coder.Desktop.App";
#else
const string appInstanceName = "Coder.Desktop.App.Debug";
#endif
return AppInstance.FindOrRegisterForKey(appInstanceName);
}
private static void ShowExceptionAndCrash(Exception e)
{
const string title = "Coder Desktop Fatal Error";
var message =
"Coder Desktop has encountered a fatal error and must exit.\n\n" +
e + "\n\n" +
Environment.StackTrace;
MessageBoxW(IntPtr.Zero, message, title, 0);
if (app != null)
app.Exit();
// This will log the exception to the Windows Event Log.
#if DEBUG
// And, if in DEBUG mode, it will also log to the console window.
AllocConsole();
#endif
Environment.FailFast("Coder Desktop has encountered a fatal error and must exit.", e);
}
}
#endif