Skip to content

Commit f5cce14

Browse files
committed
Enabled exception propagation from WinRT callbacks
1 parent 7f2d742 commit f5cce14

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

CommunityToolkit.WinUI/Extensions/DispatcherQueueExtensions{T}.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ public static unsafe bool TryEnqueue<T1, T2>(this DispatcherQueue dispatcherQueu
112112
/// <exception cref="Exception">Thrown when the enqueue operation fails.</exception>
113113
private static unsafe bool TryEnqueue(DispatcherQueue dispatcherQueue, IDispatcherQueueHandler* dispatcherQueueHandler)
114114
{
115-
IDispatcherQueue* dispatcherQueuePtr = (IDispatcherQueue*)((IWinRTObject)dispatcherQueue).NativeObject.ThisPtr;
116-
117115
bool success;
118116
int hResult;
119117

120118
try
121119
{
120+
IDispatcherQueue* dispatcherQueuePtr = (IDispatcherQueue*)((IWinRTObject)dispatcherQueue).NativeObject.ThisPtr;
121+
122122
hResult = dispatcherQueuePtr->TryEnqueue(dispatcherQueueHandler, (byte*)&success);
123123

124124
GC.KeepAlive(dispatcherQueue);
@@ -146,13 +146,13 @@ private static unsafe bool TryEnqueue(DispatcherQueue dispatcherQueue, IDispatch
146146
/// <exception cref="Exception">Thrown when the enqueue operation fails.</exception>
147147
private static unsafe bool TryEnqueue(DispatcherQueue dispatcherQueue, DispatcherQueuePriority priority, IDispatcherQueueHandler* dispatcherQueueHandler)
148148
{
149-
IDispatcherQueue* dispatcherQueuePtr = (IDispatcherQueue*)((IWinRTObject)dispatcherQueue).NativeObject.ThisPtr;
150-
151149
bool success;
152150
int hResult;
153151

154152
try
155153
{
154+
IDispatcherQueue* dispatcherQueuePtr = (IDispatcherQueue*)((IWinRTObject)dispatcherQueue).NativeObject.ThisPtr;
155+
156156
hResult = dispatcherQueuePtr->TryEnqueueWithPriority(priority, dispatcherQueueHandler, (byte*)&success);
157157

158158
GC.KeepAlive(dispatcherQueue);

CommunityToolkit.WinUI/Extensions/Interop/DispatcherQueueProxyHandler1.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Runtime.CompilerServices;
77
using System.Runtime.InteropServices;
88
using System.Threading;
9+
using WinRT;
910
using static CommunityToolkit.WinUI.Interop.Windows;
1011

1112
#nullable enable
@@ -179,8 +180,11 @@ public static int Invoke(DispatcherQueueProxyHandler1* @this)
179180
// need to make the proxy type itself generic, so without knowing the actual type argument.
180181
Unsafe.As<DispatcherQueueHandler<object>>(callback)(state);
181182
}
182-
catch
183+
catch (Exception e)
183184
{
185+
ExceptionHelpers.SetErrorInfo(e);
186+
187+
return ExceptionHelpers.GetHRForException(e);
184188
}
185189

186190
return S_OK;

CommunityToolkit.WinUI/Extensions/Interop/DispatcherQueueProxyHandler2.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Runtime.CompilerServices;
77
using System.Runtime.InteropServices;
88
using System.Threading;
9+
using WinRT;
910
using static CommunityToolkit.WinUI.Interop.Windows;
1011

1112
#nullable enable
@@ -184,8 +185,11 @@ public static int Invoke(DispatcherQueueProxyHandler2* @this)
184185
// Same optimization as in DispatcherQueueProxyHandler1
185186
Unsafe.As<DispatcherQueueHandler<object, object>>(callback)(state1, state2);
186187
}
187-
catch
188+
catch (Exception e)
188189
{
190+
ExceptionHelpers.SetErrorInfo(e);
191+
192+
return ExceptionHelpers.GetHRForException(e);
189193
}
190194

191195
return S_OK;

0 commit comments

Comments
 (0)