@@ -14,10 +14,21 @@ namespace CommunityToolkit.WinUI
14
14
/// <summary>
15
15
/// A callback that will be executed on the <see cref="DispatcherQueue"/> thread.
16
16
/// </summary>
17
- /// <typeparam name="TState ">The type of state to receive as input.</typeparam>
17
+ /// <typeparam name="T ">The type of state to receive as input.</typeparam>
18
18
/// <param name="state">The input state for the callback.</param>
19
- public delegate void DispatcherQueueHandler < in TState > ( TState state )
20
- where TState : class ;
19
+ public delegate void DispatcherQueueHandler < in T > ( T state )
20
+ where T : class ;
21
+
22
+ /// <summary>
23
+ /// A callback that will be executed on the <see cref="DispatcherQueue"/> thread.
24
+ /// </summary>
25
+ /// <typeparam name="T1">The type of the first state to receive as input.</typeparam>
26
+ /// <typeparam name="T2">The type of the second state to receive as input.</typeparam>
27
+ /// <param name="state1">The first input state for the callback.</param>
28
+ /// <param name="state2">The second input state for the callback.</param>
29
+ public delegate void DispatcherQueueHandler < in T1 , in T2 > ( T1 state1 , T2 state2 )
30
+ where T1 : class
31
+ where T2 : class ;
21
32
22
33
/// <summary>
23
34
/// Helpers for executing code in a <see cref="DispatcherQueue"/>.
@@ -27,17 +38,17 @@ public static partial class DispatcherQueueExtensions
27
38
/// <summary>
28
39
/// Adds a task to the <see cref="DispatcherQueue"/> which will be executed on the thread associated with it.
29
40
/// </summary>
30
- /// <typeparam name="TState ">The type of state to capture.</typeparam>
41
+ /// <typeparam name="T ">The type of state to capture.</typeparam>
31
42
/// <param name="dispatcherQueue">The target <see cref="DispatcherQueue"/> to invoke the code on.</param>
32
- /// <param name="callback">The input <see cref="DispatcherQueueHandler{TState }"/> callback to enqueue.</param>
43
+ /// <param name="callback">The input <see cref="DispatcherQueueHandler{T }"/> callback to enqueue.</param>
33
44
/// <param name="state">The input state to capture and pass to the callback.</param>
34
45
/// <returns>Whether or not the task was added to the queue.</returns>
35
46
/// <exception cref="Exception">Thrown when the enqueue operation fails.</exception>
36
- public static unsafe bool TryEnqueue < TState > ( this DispatcherQueue dispatcherQueue , DispatcherQueueHandler < TState > callback , TState state )
37
- where TState : class
47
+ public static unsafe bool TryEnqueue < T > ( this DispatcherQueue dispatcherQueue , DispatcherQueueHandler < T > callback , T state )
48
+ where T : class
38
49
{
39
50
IDispatcherQueue * dispatcherQueuePtr = ( IDispatcherQueue * ) ( ( IWinRTObject ) dispatcherQueue ) . NativeObject . ThisPtr ;
40
- DispatcherQueueProxyHandler * dispatcherQueueHandlerPtr = DispatcherQueueProxyHandler . Create ( callback , state ) ;
51
+ DispatcherQueueProxyHandler1 * dispatcherQueueHandlerPtr = DispatcherQueueProxyHandler1 . Create ( callback , state ) ;
41
52
42
53
bool success ;
43
54
int hResult ;
@@ -64,18 +75,99 @@ public static unsafe bool TryEnqueue<TState>(this DispatcherQueue dispatcherQueu
64
75
/// <summary>
65
76
/// Adds a task to the <see cref="DispatcherQueue"/> which will be executed on the thread associated with it.
66
77
/// </summary>
67
- /// <typeparam name="TState ">The type of state to capture.</typeparam>
78
+ /// <typeparam name="T ">The type of state to capture.</typeparam>
68
79
/// <param name="dispatcherQueue">The target <see cref="DispatcherQueue"/> to invoke the code on.</param>
69
80
/// <param name="priority"> The desired priority for the callback to schedule.</param>
70
- /// <param name="callback">The input <see cref="DispatcherQueueHandler{TState }"/> callback to enqueue.</param>
81
+ /// <param name="callback">The input <see cref="DispatcherQueueHandler{T }"/> callback to enqueue.</param>
71
82
/// <param name="state">The input state to capture and pass to the callback.</param>
72
83
/// <returns>Whether or not the task was added to the queue.</returns>
73
84
/// <exception cref="Exception">Thrown when the enqueue operation fails.</exception>
74
- public static unsafe bool TryEnqueue < TState > ( this DispatcherQueue dispatcherQueue , DispatcherQueuePriority priority , DispatcherQueueHandler < TState > callback , TState state )
75
- where TState : class
85
+ public static unsafe bool TryEnqueue < T > ( this DispatcherQueue dispatcherQueue , DispatcherQueuePriority priority , DispatcherQueueHandler < T > callback , T state )
86
+ where T : class
87
+ {
88
+ IDispatcherQueue * dispatcherQueuePtr = ( IDispatcherQueue * ) ( ( IWinRTObject ) dispatcherQueue ) . NativeObject . ThisPtr ;
89
+ DispatcherQueueProxyHandler1 * dispatcherQueueHandlerPtr = DispatcherQueueProxyHandler1 . Create ( callback , state ) ;
90
+
91
+ bool success ;
92
+ int hResult ;
93
+
94
+ try
95
+ {
96
+ hResult = dispatcherQueuePtr ->TryEnqueueWithPriority ( priority , dispatcherQueueHandlerPtr , ( byte * ) & success ) ;
97
+
98
+ GC . KeepAlive ( dispatcherQueue ) ;
99
+ }
100
+ finally
101
+ {
102
+ dispatcherQueueHandlerPtr ->Release ( ) ;
103
+ }
104
+
105
+ if ( hResult != 0 )
106
+ {
107
+ ExceptionHelpers . ThrowExceptionForHR ( hResult ) ;
108
+ }
109
+
110
+ return success ;
111
+ }
112
+
113
+ /// <summary>
114
+ /// Adds a task to the <see cref="DispatcherQueue"/> which will be executed on the thread associated with it.
115
+ /// </summary>
116
+ /// <typeparam name="T1">The type of the first state to capture.</typeparam>
117
+ /// <typeparam name="T2">The type of the second state to capture.</typeparam>
118
+ /// <param name="dispatcherQueue">The target <see cref="DispatcherQueue"/> to invoke the code on.</param>
119
+ /// <param name="callback">The input <see cref="DispatcherQueueHandler{T}"/> callback to enqueue.</param>
120
+ /// <param name="state1">The first input state to capture and pass to the callback.</param>
121
+ /// <param name="state2">The second input state to capture and pass to the callback.</param>
122
+ /// <returns>Whether or not the task was added to the queue.</returns>
123
+ /// <exception cref="Exception">Thrown when the enqueue operation fails.</exception>
124
+ public static unsafe bool TryEnqueue < T1 , T2 > ( this DispatcherQueue dispatcherQueue , DispatcherQueueHandler < T1 , T2 > callback , T1 state1 , T2 state2 )
125
+ where T1 : class
126
+ where T2 : class
127
+ {
128
+ IDispatcherQueue * dispatcherQueuePtr = ( IDispatcherQueue * ) ( ( IWinRTObject ) dispatcherQueue ) . NativeObject . ThisPtr ;
129
+ DispatcherQueueProxyHandler2 * dispatcherQueueHandlerPtr = DispatcherQueueProxyHandler2 . Create ( callback , state1 , state2 ) ;
130
+
131
+ bool success ;
132
+ int hResult ;
133
+
134
+ try
135
+ {
136
+ hResult = dispatcherQueuePtr ->TryEnqueue ( dispatcherQueueHandlerPtr , ( byte * ) & success ) ;
137
+
138
+ GC . KeepAlive ( dispatcherQueue ) ;
139
+ }
140
+ finally
141
+ {
142
+ dispatcherQueueHandlerPtr ->Release ( ) ;
143
+ }
144
+
145
+ if ( hResult != 0 )
146
+ {
147
+ ExceptionHelpers . ThrowExceptionForHR ( hResult ) ;
148
+ }
149
+
150
+ return success ;
151
+ }
152
+
153
+ /// <summary>
154
+ /// Adds a task to the <see cref="DispatcherQueue"/> which will be executed on the thread associated with it.
155
+ /// </summary>
156
+ /// <typeparam name="T1">The type of the first state to capture.</typeparam>
157
+ /// <typeparam name="T2">The type of the second state to capture.</typeparam>
158
+ /// <param name="dispatcherQueue">The target <see cref="DispatcherQueue"/> to invoke the code on.</param>
159
+ /// <param name="priority"> The desired priority for the callback to schedule.</param>
160
+ /// <param name="callback">The input <see cref="DispatcherQueueHandler{T}"/> callback to enqueue.</param>
161
+ /// <param name="state1">The first input state to capture and pass to the callback.</param>
162
+ /// <param name="state2">The second input state to capture and pass to the callback.</param>
163
+ /// <returns>Whether or not the task was added to the queue.</returns>
164
+ /// <exception cref="Exception">Thrown when the enqueue operation fails.</exception>
165
+ public static unsafe bool TryEnqueue < T1 , T2 > ( this DispatcherQueue dispatcherQueue , DispatcherQueuePriority priority , DispatcherQueueHandler < T1 , T2 > callback , T1 state1 , T2 state2 )
166
+ where T1 : class
167
+ where T2 : class
76
168
{
77
169
IDispatcherQueue * dispatcherQueuePtr = ( IDispatcherQueue * ) ( ( IWinRTObject ) dispatcherQueue ) . NativeObject . ThisPtr ;
78
- DispatcherQueueProxyHandler * dispatcherQueueHandlerPtr = DispatcherQueueProxyHandler . Create ( callback , state ) ;
170
+ DispatcherQueueProxyHandler2 * dispatcherQueueHandlerPtr = DispatcherQueueProxyHandler2 . Create ( callback , state1 , state2 ) ;
79
171
80
172
bool success ;
81
173
int hResult ;
0 commit comments