-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
121 lines (109 loc) · 4.63 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace PPTXcreator
{
public static class Program
{
public static Window MainWindow { get; private set; }
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
public static void Main()
{
// Configure error handling
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(CrashHandlerUI);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CrashHandlerDomain);
// Load settings, service information, songnames from disk
Settings.Load();
Service.UpdateJsonCache();
Songnames.LoadNames();
// Check for updates
if (Settings.Instance.EnableUpdateChecker) Task.Run(() => UpdateChecker.CheckReleases());
// start UI
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainWindow = new Window();
Application.Run(MainWindow);
// Save settings when closing
Settings.Save();
}
/// <summary>
/// Handle unhandled exceptions in the UI thread
/// </summary>
private static void CrashHandlerUI(object sender, System.Threading.ThreadExceptionEventArgs args)
{
Dialogs.CrashNotification(args.Exception.Message + "\n\nStack Trace: " + args.Exception.StackTrace);
Application.Exit();
}
/// <summary>
/// Handle unhandled exceptions which are not in the UI thread
/// </summary>
private static void CrashHandlerDomain(object sender, UnhandledExceptionEventArgs args)
{
Exception exception = (Exception)args.ExceptionObject;
Dialogs.CrashNotification(exception.Message + "\n\nStack Trace: " + exception.StackTrace);
Application.Exit();
}
/// <summary>
/// Helper function for loading files and handling exceptions
/// </summary>
/// <param name="path">Path to the file that has to be read</param>
/// <param name="filecontents">The contents of the file, or an empty string if reading failed</param>
/// <returns>Whether the function succeeded</returns>
public static bool TryGetFileContents(string path, out string filecontents)
{
filecontents = "";
try
{
filecontents = File.ReadAllText(path);
return true;
}
catch (Exception ex) when (ex is DirectoryNotFoundException || ex is FileNotFoundException)
{
Dialogs.GenericWarning($"'{path}' kon niet worden geopend omdat het bestand niet gevonden is.");
return false;
}
catch (Exception ex) when (ex is IOException
|| ex is UnauthorizedAccessException
|| ex is NotSupportedException
|| ex is System.Security.SecurityException)
{
Dialogs.GenericWarning($"'{path}' kon niet worden geopend.\n\n" +
$"De volgende foutmelding werd gegeven: {ex.Message}");
return false;
}
}
/// <summary>
/// Helper function for opening a filestream and handling exceptions
/// </summary>
/// <param name="path">Path to the file that has to be read</param>
/// <param name="stream">The filestream, or null if reading failed</param>
/// <returns>Whether the function succeeded</returns>
public static bool TryGetFileStream(string path, out FileStream stream)
{
stream = null;
try
{
stream = File.OpenRead(path);
return true;
}
catch (IOException ex) when (ex is DirectoryNotFoundException || ex is FileNotFoundException)
{
Dialogs.GenericWarning($"'{path}' kon niet worden geopend omdat het bestand niet gevonden is.");
return false;
}
catch (Exception ex) when (ex is IOException
|| ex is UnauthorizedAccessException
|| ex is NotSupportedException)
{
Dialogs.GenericWarning($"'{path}' kon niet worden geopend.\n\n" +
$"De volgende foutmelding werd gegeven: {ex.Message}");
return false;
}
}
}
}