Repository navigation
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
187 lines (163 loc) · 6.95 KB
/
Copy pathApp.xaml.cs
File metadata and controls
187 lines (163 loc) · 6.95 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using SystemMonitorApp.Services;
namespace SystemMonitorApp
{
public partial class App : Application
{
private const int MinSplashMs = 400;
private SplashWindow? _splashWindow;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
RegisterGlobalExceptionHandlers();
Logger.Info($"SystemFlow Pro starting. Version={GetAssemblyVersion()}, OS={Environment.OSVersion}, .NET={Environment.Version}");
_ = SettingsService.Current; // trigger static init so settings load before any consumers
_splashWindow = new SplashWindow();
_splashWindow.Show();
_ = StartMainWindowAsync();
}
private async Task StartMainWindowAsync()
{
var splashStarted = DateTime.UtcNow;
MainWindow? mainWindow = null;
bool startupFailed = false;
try
{
// Create window on UI thread (minimal — constructor does nothing heavy).
await Dispatcher.InvokeAsync(() =>
{
mainWindow = new MainWindow();
MainWindow = mainWindow;
});
if (mainWindow == null)
throw new InvalidOperationException("MainWindow creation returned null");
// Heavy init happens on background thread now — splash can stay interactive.
await mainWindow.InitializeAsync();
// Ensure splash is visible at least MinSplashMs so it doesn't flash.
var elapsed = (DateTime.UtcNow - splashStarted).TotalMilliseconds;
if (elapsed < MinSplashMs)
await Task.Delay(MinSplashMs - (int)elapsed);
await Dispatcher.InvokeAsync(() =>
{
if (SettingsService.Current.StartMinimized)
mainWindow.WindowState = WindowState.Minimized;
mainWindow.Show();
});
// Tiny settle time so the main window renders before splash fades.
await Task.Delay(150);
Logger.Info($"Main window shown successfully (total startup {((DateTime.UtcNow - splashStarted).TotalMilliseconds):F0}ms)");
// Fire-and-forget tasks AFTER window is shown — these no longer add to startup time.
mainWindow.ScheduleBackgroundDiagnostics();
_ = CheckForUpdatesInBackground();
}
catch (Exception ex)
{
startupFailed = true;
Logger.Error("Startup sequence failed", ex);
await Dispatcher.InvokeAsync(() =>
{
MessageBox.Show(
$"An error occurred during startup of SystemFlow Pro.\n\n{ex.Message}\n\nSee the log in %APPDATA%\\SystemFlow Pro\\logs for details.",
"Startup Error",
MessageBoxButton.OK,
MessageBoxImage.Error);
});
}
finally
{
// Splash MUST close regardless of success/failure — otherwise user sees both.
try
{
await Dispatcher.InvokeAsync(() => _splashWindow?.CloseSplash());
}
catch (Exception ex) { Logger.Warn("Splash close failed", ex); }
if (startupFailed)
await Dispatcher.InvokeAsync(() => Shutdown(1));
}
}
private async Task CheckForUpdatesInBackground()
{
try
{
// Wait a moment so the user sees the UI before any notification pops.
await Task.Delay(TimeSpan.FromSeconds(8));
var info = await UpdateChecker.CheckAsync();
if (info == null) return;
await Dispatcher.InvokeAsync(() =>
{
var result = MessageBox.Show(
$"New version available: {info.LatestVersion}\n\nDo you want to open the release page?",
"SystemFlow Pro — update available",
MessageBoxButton.YesNo,
MessageBoxImage.Information);
if (result == MessageBoxResult.Yes && !string.IsNullOrEmpty(info.ReleaseUrl))
{
try
{
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
{
FileName = info.ReleaseUrl,
UseShellExecute = true
});
}
catch (Exception ex) { Logger.Warn("Open release URL failed", ex); }
}
});
}
catch (Exception ex)
{
Logger.Info($"Update check background task failed (silent): {ex.Message}");
}
}
private void RegisterGlobalExceptionHandlers()
{
DispatcherUnhandledException += (s, e) =>
{
Logger.Error("Unhandled UI exception", e.Exception);
try
{
MessageBox.Show(
$"An unexpected error occurred.\n\n{e.Exception.Message}\n\n" +
"The error has been saved to the log:\n%APPDATA%\\SystemFlow Pro\\logs",
"SystemFlow Pro",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
catch { }
e.Handled = true;
};
TaskScheduler.UnobservedTaskException += (s, e) =>
{
Logger.Error("Unobserved task exception", e.Exception);
e.SetObserved();
};
AppDomain.CurrentDomain.UnhandledException += (s, e) =>
{
if (e.ExceptionObject is Exception ex)
Logger.Error($"AppDomain unhandled exception (terminating={e.IsTerminating})", ex);
else
Logger.Error($"AppDomain unhandled non-exception (terminating={e.IsTerminating})");
Logger.Flush();
};
}
protected override void OnExit(ExitEventArgs e)
{
Logger.Info($"SystemFlow Pro exiting. Code={e.ApplicationExitCode}");
Logger.Flush();
Logger.Shutdown();
base.OnExit(e);
}
private static string GetAssemblyVersion()
{
try
{
return System.Reflection.Assembly.GetExecutingAssembly()
.GetName().Version?.ToString() ?? "unknown";
}
catch { return "unknown"; }
}
}
}