Skip to content

Commit a3370ed

Browse files
committed
exceptions seem to properly propagate now
1 parent 8ac5d7b commit a3370ed

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

GPUPrefSwitcher.sln

-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ Global
151151
{76BFD458-C88C-4484-9578-DC1B75BB945C}.Debug|Any CPU.ActiveCfg = Debug
152152
{76BFD458-C88C-4484-9578-DC1B75BB945C}.Debug|ARM64.ActiveCfg = Debug
153153
{76BFD458-C88C-4484-9578-DC1B75BB945C}.Debug|x64.ActiveCfg = Debug
154-
{76BFD458-C88C-4484-9578-DC1B75BB945C}.Debug|x64.Build.0 = Debug
155154
{76BFD458-C88C-4484-9578-DC1B75BB945C}.Debug|x86.ActiveCfg = Debug
156155
{76BFD458-C88C-4484-9578-DC1B75BB945C}.Release|Any CPU.ActiveCfg = Release
157156
{76BFD458-C88C-4484-9578-DC1B75BB945C}.Release|ARM64.ActiveCfg = Release

GPUPrefSwitcher/AppLogger.cs

+2
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public static AppLogger Initialize (string logFolderPath_Error, string logFileNa
104104
//Might trigger StackOverflow if the functions inside this errors (log error -> error -> log error -> error ...)
105105
AppDomain.CurrentDomain.UnhandledException += (sender, e) => //Only handles uncaught ones (that terminate the app), which is what we want
106106
{
107+
logger.Log("An unhandled error has occurred, check the Error Log");
107108
logger.DumpStandardLogBufferToStandardLog().Wait();
108109
logger.DumpStandardLogBufferToErrorLog().Wait(); //do this before the app terminates
109110
logger.ErrorLog("<<<AN UNHANDLED ERROR HAS OCCURRED>>>");
@@ -114,6 +115,7 @@ public static AppLogger Initialize (string logFolderPath_Error, string logFileNa
114115
///TODO this doesn't work?
115116
System.Threading.Tasks.TaskScheduler.UnobservedTaskException += (sender, e) =>
116117
{
118+
logger.Log("An unobserved task exception has occurred, check the Error Log");
117119
logger.DumpStandardLogBufferToStandardLog().Wait();
118120
logger.DumpStandardLogBufferToErrorLog().Wait(); //do this before the app terminates
119121
logger.ErrorLog("<<<AN UNOBSERVED TASK EXCEPTION HAS OCCURRED>>>");

GPUPrefSwitcher/Switcher.cs

+9-6
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static class Switcher
4141
/// <summary>
4242
/// Call once upon service start. Initializes necessary components and starts the timer.
4343
/// </summary>
44-
internal static void Start()
44+
internal static void Start(CancellationToken cancellationToken)
4545
{
4646
appOptions = new AppOptions();
4747
Logger.inst.EnableRealtimeStandardLogWrites = appOptions.CurrentOptions.EnableRealtimeLogging;
@@ -70,7 +70,7 @@ internal static void Start()
7070

7171
File.Delete(CRASHED_FILE_PATH);//"successful" initialization yippee
7272

73-
Task forever = BeginTimerForever();
73+
Task forever = BeginTimerForever(cancellationToken);
7474

7575
/*
7676
forever.ContinueWith(
@@ -119,15 +119,16 @@ internal static void Stop()
119119
/// <summary>
120120
/// There should be only one of this running at any time.
121121
/// </summary>
122-
private static async Task BeginTimerForever()
122+
private static Task BeginTimerForever(CancellationToken cancellationToken)
123123
{
124124
if (timerRunning) { throw new InvalidOperationException("This function cannot be called more than once"); }
125125
timerRunning = true;
126-
while (true)
126+
while (!cancellationToken.IsCancellationRequested)
127127
{
128128

129-
foreach(Task task in runningUpdateTasks)
129+
for (int i = runningUpdateTasks.Count - 1; i >= 0; i--)
130130
{
131+
Task task = runningUpdateTasks[i];
131132
//TODO run a new task even if the previous one is going
132133
if (task != null && task.IsFaulted)
133134
{
@@ -145,8 +146,10 @@ private static async Task BeginTimerForever()
145146

146147
runningUpdateTasks.Add(RunUpdateLogic());
147148

148-
await Task.Delay(updateInterval); //move this to the beginning to make debugging easier
149+
Task.Delay(updateInterval).Wait(); //move this to the beginning to make debugging easier
149150
}
151+
152+
return Task.CompletedTask;
150153
}
151154

152155
private static async Task RunUpdateLogic()

GPUPrefSwitcher/SwitcherService.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ public SwitcherService(ILogger<SwitcherService> logger)
1919

2020
public Task StartAsync(CancellationToken cancellationToken)
2121
{
22-
23-
Switcher.Start();
24-
2522
SystemEvents.SessionEnding += OnSessionEnd;
2623

24+
Switcher.Start(cancellationToken);
25+
2726
return Task.CompletedTask;
2827
}
2928

0 commit comments

Comments
 (0)