Skip to content

Commit 4755472

Browse files
committed
Minor code refactoring
1 parent 20a6e9a commit 4755472

File tree

1 file changed

+12
-38
lines changed

1 file changed

+12
-38
lines changed

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

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static partial class DispatcherQueueExtensions
4747
public static unsafe bool TryEnqueue<T>(this DispatcherQueue dispatcherQueue, DispatcherQueueHandler<T> callback, T state)
4848
where T : class
4949
{
50-
return TryEnqueue(dispatcherQueue, DispatcherQueueProxyHandler1.Create(callback, state));
50+
return TryEnqueue(dispatcherQueue, null, DispatcherQueueProxyHandler1.Create(callback, state));
5151
}
5252

5353
/// <summary>
@@ -81,7 +81,7 @@ public static unsafe bool TryEnqueue<T1, T2>(this DispatcherQueue dispatcherQueu
8181
where T1 : class
8282
where T2 : class
8383
{
84-
return TryEnqueue(dispatcherQueue, DispatcherQueueProxyHandler2.Create(callback, state1, state2));
84+
return TryEnqueue(dispatcherQueue, null, DispatcherQueueProxyHandler2.Create(callback, state1, state2));
8585
}
8686

8787
/// <summary>
@@ -107,10 +107,11 @@ public static unsafe bool TryEnqueue<T1, T2>(this DispatcherQueue dispatcherQueu
107107
/// Adds a task to the <see cref="DispatcherQueue"/> which will be executed on the thread associated with it.
108108
/// </summary>
109109
/// <param name="dispatcherQueue">The target <see cref="DispatcherQueue"/> to invoke the code on.</param>
110+
/// <param name="priority"> The desired priority for the callback to schedule (if available).</param>
110111
/// <param name="dispatcherQueueHandler">The input callback to enqueue.</param>
111112
/// <returns>Whether or not the task was added to the queue.</returns>
112113
/// <exception cref="Exception">Thrown when the enqueue operation fails.</exception>
113-
private static unsafe bool TryEnqueue(DispatcherQueue dispatcherQueue, IDispatcherQueueHandler* dispatcherQueueHandler)
114+
private static unsafe bool TryEnqueue(DispatcherQueue dispatcherQueue, DispatcherQueuePriority? priority, IDispatcherQueueHandler* dispatcherQueueHandler)
114115
{
115116
bool success;
116117
int hResult;
@@ -119,41 +120,14 @@ private static unsafe bool TryEnqueue(DispatcherQueue dispatcherQueue, IDispatch
119120
{
120121
IDispatcherQueue* dispatcherQueuePtr = (IDispatcherQueue*)((IWinRTObject)dispatcherQueue).NativeObject.ThisPtr;
121122

122-
hResult = dispatcherQueuePtr->TryEnqueue(dispatcherQueueHandler, (byte*)&success);
123-
124-
GC.KeepAlive(dispatcherQueue);
125-
}
126-
finally
127-
{
128-
dispatcherQueueHandler->Release();
129-
}
130-
131-
if (hResult != 0)
132-
{
133-
ExceptionHelpers.ThrowExceptionForHR(hResult);
134-
}
135-
136-
return success;
137-
}
138-
139-
/// <summary>
140-
/// Adds a task to the <see cref="DispatcherQueue"/> which will be executed on the thread associated with it.
141-
/// </summary>
142-
/// <param name="dispatcherQueue">The target <see cref="DispatcherQueue"/> to invoke the code on.</param>
143-
/// <param name="priority"> The desired priority for the callback to schedule.</param>
144-
/// <param name="dispatcherQueueHandler">The input callback to enqueue.</param>
145-
/// <returns>Whether or not the task was added to the queue.</returns>
146-
/// <exception cref="Exception">Thrown when the enqueue operation fails.</exception>
147-
private static unsafe bool TryEnqueue(DispatcherQueue dispatcherQueue, DispatcherQueuePriority priority, IDispatcherQueueHandler* dispatcherQueueHandler)
148-
{
149-
bool success;
150-
int hResult;
151-
152-
try
153-
{
154-
IDispatcherQueue* dispatcherQueuePtr = (IDispatcherQueue*)((IWinRTObject)dispatcherQueue).NativeObject.ThisPtr;
155-
156-
hResult = dispatcherQueuePtr->TryEnqueueWithPriority(priority, dispatcherQueueHandler, (byte*)&success);
123+
if (priority.HasValue)
124+
{
125+
hResult = dispatcherQueuePtr->TryEnqueueWithPriority(priority.GetValueOrDefault(), dispatcherQueueHandler, (byte*)&success);
126+
}
127+
else
128+
{
129+
hResult = dispatcherQueuePtr->TryEnqueue(dispatcherQueueHandler, (byte*)&success);
130+
}
157131

158132
GC.KeepAlive(dispatcherQueue);
159133
}

0 commit comments

Comments
 (0)