Skip to content

Commit 9e518af

Browse files
committed
Added exception handling for WinRT calls
1 parent a815f39 commit 9e518af

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,19 @@ public static partial class DispatcherQueueExtensions
3636
/// <param name="callback">The input <see cref="DispatcherQueueHandler{TState}"/> callback to enqueue.</param>
3737
/// <param name="state">The input state to capture and pass to the callback.</param>
3838
/// <returns>Whether or not the task was added to the queue.</returns>
39+
/// <exception cref="Exception">Thrown when the enqueue operation fails.</exception>
3940
public static unsafe bool TryEnqueue<TState>(this DispatcherQueue dispatcherQueue, DispatcherQueueHandler<TState> callback, TState state)
4041
where TState : class
4142
{
4243
IDispatcherQueue* dispatcherQueuePtr = (IDispatcherQueue*)((IWinRTObject)dispatcherQueue).NativeObject.ThisPtr;
4344
DispatcherQueueProxyHandler* dispatcherQueueHandlerPtr = DispatcherQueueProxyHandler.Create(callback, state);
4445

45-
byte result;
46+
bool success;
47+
int hResult;
4648

4749
try
4850
{
49-
_ = dispatcherQueuePtr->TryEnqueue(dispatcherQueueHandlerPtr, &result);
51+
hResult = dispatcherQueuePtr->TryEnqueue(dispatcherQueueHandlerPtr, (byte*)&success);
5052

5153
GC.KeepAlive(dispatcherQueue);
5254
}
@@ -55,7 +57,12 @@ public static unsafe bool TryEnqueue<TState>(this DispatcherQueue dispatcherQueu
5557
dispatcherQueueHandlerPtr->Release();
5658
}
5759

58-
return result == 0;
60+
if (hResult != 0)
61+
{
62+
ExceptionHelpers.ThrowExceptionForHR(hResult);
63+
}
64+
65+
return success;
5966
}
6067

6168
/// <summary>
@@ -67,17 +74,19 @@ public static unsafe bool TryEnqueue<TState>(this DispatcherQueue dispatcherQueu
6774
/// <param name="callback">The input <see cref="DispatcherQueueHandler{TState}"/> callback to enqueue.</param>
6875
/// <param name="state">The input state to capture and pass to the callback.</param>
6976
/// <returns>Whether or not the task was added to the queue.</returns>
77+
/// <exception cref="Exception">Thrown when the enqueue operation fails.</exception>
7078
public static unsafe bool TryEnqueue<TState>(this DispatcherQueue dispatcherQueue, DispatcherQueuePriority priority, DispatcherQueueHandler<TState> callback, TState state)
7179
where TState : class
7280
{
7381
IDispatcherQueue* dispatcherQueuePtr = (IDispatcherQueue*)((IWinRTObject)dispatcherQueue).NativeObject.ThisPtr;
7482
DispatcherQueueProxyHandler* dispatcherQueueHandlerPtr = DispatcherQueueProxyHandler.Create(callback, state);
7583

76-
byte result;
84+
bool success;
85+
int hResult;
7786

7887
try
7988
{
80-
_ = dispatcherQueuePtr->TryEnqueueWithPriority(priority, dispatcherQueueHandlerPtr, &result);
89+
hResult = dispatcherQueuePtr->TryEnqueueWithPriority(priority, dispatcherQueueHandlerPtr, (byte*)&success);
8190

8291
GC.KeepAlive(dispatcherQueue);
8392
}
@@ -86,7 +95,12 @@ public static unsafe bool TryEnqueue<TState>(this DispatcherQueue dispatcherQueu
8695
dispatcherQueueHandlerPtr->Release();
8796
}
8897

89-
return result == 0;
98+
if (hResult != 0)
99+
{
100+
ExceptionHelpers.ThrowExceptionForHR(hResult);
101+
}
102+
103+
return success;
90104
}
91105

92106
/// <summary>

0 commit comments

Comments
 (0)