Skip to content

Commit 275d277

Browse files
authored
Fix: Fixed COMException loop due to failure to generate notification (#14754)
1 parent b426e64 commit 275d277

File tree

3 files changed

+57
-48
lines changed

3 files changed

+57
-48
lines changed

src/Files.App/App.xaml.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,15 @@ private async void Window_Closed(object sender, WindowEventArgs args)
239239
// Displays a notification the first time the app goes to the background
240240
if (userSettingsService.AppSettingsService.ShowBackgroundRunningNotification)
241241
{
242-
userSettingsService.AppSettingsService.ShowBackgroundRunningNotification = false;
243-
244-
var toastContent = new ToastContent()
242+
SafetyExtensions.IgnoreExceptions(() =>
245243
{
246-
Visual = new()
244+
var toastContent = new ToastContent()
247245
{
248-
BindingGeneric = new ToastBindingGeneric()
246+
Visual = new()
249247
{
250-
Children =
248+
BindingGeneric = new ToastBindingGeneric()
249+
{
250+
Children =
251251
{
252252
new AdaptiveText()
253253
{
@@ -258,16 +258,19 @@ private async void Window_Closed(object sender, WindowEventArgs args)
258258
Text = "BackgroundRunningNotificationBody".GetLocalizedResource()
259259
}
260260
},
261-
}
262-
},
263-
ActivationType = ToastActivationType.Protocol
264-
};
261+
}
262+
},
263+
ActivationType = ToastActivationType.Protocol
264+
};
265+
266+
// Create the toast notification
267+
var toastNotification = new ToastNotification(toastContent.GetXml());
265268

266-
// Create the toast notification
267-
var toastNotification = new ToastNotification(toastContent.GetXml());
269+
// And send the notification
270+
ToastNotificationManager.CreateToastNotifier().Show(toastNotification);
268271

269-
// And send the notification
270-
ToastNotificationManager.CreateToastNotifier().Show(toastNotification);
272+
userSettingsService.AppSettingsService.ShowBackgroundRunningNotification = false;
273+
});
271274
}
272275

273276
if (Program.Pool.WaitOne())

src/Files.App/Helpers/Application/AppLifecycleHelper.cs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,15 @@ public static void HandleAppUnhandledException(Exception? ex, bool showToastNoti
288288
if (!showToastNotification)
289289
return;
290290

291-
var toastContent = new ToastContent()
291+
SafetyExtensions.IgnoreExceptions(() =>
292292
{
293-
Visual = new()
293+
var toastContent = new ToastContent()
294294
{
295-
BindingGeneric = new ToastBindingGeneric()
295+
Visual = new()
296296
{
297-
Children =
297+
BindingGeneric = new ToastBindingGeneric()
298+
{
299+
Children =
298300
{
299301
new AdaptiveText()
300302
{
@@ -305,30 +307,31 @@ public static void HandleAppUnhandledException(Exception? ex, bool showToastNoti
305307
Text = "ExceptionNotificationBody".GetLocalizedResource()
306308
}
307309
},
308-
AppLogoOverride = new()
309-
{
310-
Source = "ms-appx:///Assets/error.png"
310+
AppLogoOverride = new()
311+
{
312+
Source = "ms-appx:///Assets/error.png"
313+
}
311314
}
312-
}
313-
},
314-
Actions = new ToastActionsCustom()
315-
{
316-
Buttons =
315+
},
316+
Actions = new ToastActionsCustom()
317+
{
318+
Buttons =
317319
{
318320
new ToastButton("ExceptionNotificationReportButton".GetLocalizedResource(), Constants.GitHub.BugReportUrl)
319321
{
320322
ActivationType = ToastActivationType.Protocol
321323
}
322324
}
323-
},
324-
ActivationType = ToastActivationType.Protocol
325-
};
325+
},
326+
ActivationType = ToastActivationType.Protocol
327+
};
326328

327-
// Create the toast notification
328-
var toastNotification = new ToastNotification(toastContent.GetXml());
329+
// Create the toast notification
330+
var toastNotification = new ToastNotification(toastContent.GetXml());
329331

330-
// And send the notification
331-
ToastNotificationManager.CreateToastNotifier().Show(toastNotification);
332+
// And send the notification
333+
ToastNotificationManager.CreateToastNotifier().Show(toastNotification);
334+
});
332335

333336
// Restart the app
334337
var userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();

src/Files.App/Helpers/UI/UIHelpers.cs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ public static async Task ShowDeviceEjectResultAsync(Data.Items.DriveType type, b
4040
{
4141
Debug.WriteLine("Device successfully ejected");
4242

43-
var toastContent = new ToastContent()
43+
SafetyExtensions.IgnoreExceptions(() =>
4444
{
45-
Visual = new ToastVisual()
45+
var toastContent = new ToastContent()
4646
{
47-
BindingGeneric = new ToastBindingGeneric()
47+
Visual = new ToastVisual()
4848
{
49-
Children =
49+
BindingGeneric = new ToastBindingGeneric()
50+
{
51+
Children =
5052
{
5153
new AdaptiveText()
5254
{
@@ -57,20 +59,21 @@ public static async Task ShowDeviceEjectResultAsync(Data.Items.DriveType type, b
5759
Text = "EjectNotificationBody".GetLocalizedResource()
5860
}
5961
},
60-
Attribution = new ToastGenericAttributionText()
61-
{
62-
Text = "SettingsAboutAppName".GetLocalizedResource()
62+
Attribution = new ToastGenericAttributionText()
63+
{
64+
Text = "SettingsAboutAppName".GetLocalizedResource()
65+
}
6366
}
64-
}
65-
},
66-
ActivationType = ToastActivationType.Protocol
67-
};
67+
},
68+
ActivationType = ToastActivationType.Protocol
69+
};
6870

69-
// Create the toast notification
70-
var toastNotif = new ToastNotification(toastContent.GetXml());
71+
// Create the toast notification
72+
var toastNotif = new ToastNotification(toastContent.GetXml());
7173

72-
// And send the notification
73-
ToastNotificationManager.CreateToastNotifier().Show(toastNotif);
74+
// And send the notification
75+
ToastNotificationManager.CreateToastNotifier().Show(toastNotif);
76+
});
7477
}
7578
else if (!result)
7679
{

0 commit comments

Comments
 (0)