/
githubmirror
/
Files
Обзор
Документация
Войти
/
githubmirror
/
Files
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Files.App/Extensions/DispatcherQueueExtensions.cs
92 строки
3 KB
yair100
Code Quality: Handle "Failed to enqueue" exception from DispatcherQueue (#18423)
24 апр 2026, 20:43
Не верифицирован
24 апр 2026, 20:43
1425850
Код
Авторство
О чём код?
using CommunityToolkit.WinUI; using Microsoft.UI.Dispatching; using System.Runtime.InteropServices; namespace Files.App.Extensions { // Window.DispatcherQueue seems to be null sometimes. // We don't know why, but as a workaround, we invoke the function directly if DispatcherQueue is null. public static class DispatcherQueueExtensions { public static Task EnqueueOrInvokeAsync(this DispatcherQueue? dispatcher, Func<Task> function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) { return SafetyExtensions.IgnoreExceptions(async () => { if (dispatcher is not null) { try { await dispatcher.EnqueueAsync(function, priority); } catch (InvalidOperationException ex) when (ex.Message is "Failed to enqueue the operation") { } return; } await function(); }, App.Logger, typeof(COMException)); } public static Task<T?> EnqueueOrInvokeAsync<T>(this DispatcherQueue? dispatcher, Func<Task<T>> function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) { return SafetyExtensions.IgnoreExceptions(async () => { if (dispatcher is not null) { try { return await dispatcher.EnqueueAsync(function, priority); } catch (InvalidOperationException ex) when (ex.Message is "Failed to enqueue the operation") { return default; } } return await function(); }, App.Logger, typeof(COMException)); } public static Task EnqueueOrInvokeAsync(this DispatcherQueue? dispatcher, Action function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) { return SafetyExtensions.IgnoreExceptions(async () => { if (dispatcher is not null) { try { await dispatcher.EnqueueAsync(function, priority); } catch (InvalidOperationException ex) when (ex.Message is "Failed to enqueue the operation") { } return; } function(); }, App.Logger, typeof(COMException)); } public static Task<T?> EnqueueOrInvokeAsync<T>(this DispatcherQueue? dispatcher, Func<T> function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) { return SafetyExtensions.IgnoreExceptions(async () => { if (dispatcher is not null) { try { return await dispatcher.EnqueueAsync(function, priority); } catch (InvalidOperationException ex) when (ex.Message is "Failed to enqueue the operation") { return default; } } return function(); }, App.Logger, typeof(COMException)); } } }