Repository navigation
Expand file tree
/
Copy pathDispatcherQueueExtensions.cs
More file actions
112 lines (102 loc) · 3.14 KB
/
Copy pathDispatcherQueueExtensions.cs
File metadata and controls
112 lines (102 loc) · 3.14 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
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
{
// Always queue so loading cannot start inside a layout or binding callback.
public static Task EnqueueDeferredAsync(this DispatcherQueue? dispatcher, Func<Task> function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal)
{
var completion = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
if (dispatcher?.TryEnqueue(priority, async () =>
{
try
{
await function();
completion.TrySetResult();
}
catch (Exception ex)
{
completion.TrySetException(ex);
}
}) is not true)
completion.TrySetResult();
return completion.Task;
}
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));
}
}
}