-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
83 lines (73 loc) · 2.29 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
using System;
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();
if (!CheckSingleInstance()) return;
Application.Start(p =>
{
var context = new DispatcherQueueSynchronizationContext(DispatcherQueue.GetForCurrentThread());
SynchronizationContext.SetSynchronizationContext(context);
app = new App();
app.UnhandledException += (_, e) =>
{
e.Handled = true;
ShowExceptionAndCrash(e.Exception);
};
});
}
catch (Exception e)
{
ShowExceptionAndCrash(e);
}
}
[STAThread]
private static bool CheckSingleInstance()
{
#if !DEBUG
const string appInstanceName = "Coder.Desktop.App";
#else
const string appInstanceName = "Coder.Desktop.App.Debug";
#endif
var instance = AppInstance.FindOrRegisterForKey(appInstanceName);
return instance.IsCurrent;
}
[STAThread]
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