33// See the LICENSE file in the project root for more information.
44
55using System ;
6+ using System . Runtime . CompilerServices ;
67using System . Threading . Tasks ;
78using Windows . Foundation . Metadata ;
89using Windows . System ;
@@ -67,7 +68,7 @@ static Task TryEnqueueAsync(DispatcherQueue dispatcher, Action function, Dispatc
6768 }
6869 } ) )
6970 {
70- taskCompletionSource . SetException ( new InvalidOperationException ( "Failed to enqueue the operation" ) ) ;
71+ taskCompletionSource . SetException ( GetEnqueueException ( "Failed to enqueue the operation" ) ) ;
7172 }
7273
7374 return taskCompletionSource . Task ;
@@ -116,7 +117,7 @@ static Task<T> TryEnqueueAsync(DispatcherQueue dispatcher, Func<T> function, Dis
116117 }
117118 } ) )
118119 {
119- taskCompletionSource . SetException ( new InvalidOperationException ( "Failed to enqueue the operation" ) ) ;
120+ taskCompletionSource . SetException ( GetEnqueueException ( "Failed to enqueue the operation" ) ) ;
120121 }
121122
122123 return taskCompletionSource . Task ;
@@ -149,7 +150,7 @@ public static Task EnqueueAsync(this DispatcherQueue dispatcher, Func<Task> func
149150 return awaitableResult ;
150151 }
151152
152- return Task . FromException ( new InvalidOperationException ( "The Task returned by function cannot be null." ) ) ;
153+ return Task . FromException ( GetEnqueueException ( "The Task returned by function cannot be null." ) ) ;
153154 }
154155 catch ( Exception e )
155156 {
@@ -173,7 +174,7 @@ static Task TryEnqueueAsync(DispatcherQueue dispatcher, Func<Task> function, Dis
173174 }
174175 else
175176 {
176- taskCompletionSource . SetException ( new InvalidOperationException ( "The Task returned by function cannot be null." ) ) ;
177+ taskCompletionSource . SetException ( GetEnqueueException ( "The Task returned by function cannot be null." ) ) ;
177178 }
178179 }
179180 catch ( Exception e )
@@ -182,7 +183,7 @@ static Task TryEnqueueAsync(DispatcherQueue dispatcher, Func<Task> function, Dis
182183 }
183184 } ) )
184185 {
185- taskCompletionSource . SetException ( new InvalidOperationException ( "Failed to enqueue the operation" ) ) ;
186+ taskCompletionSource . SetException ( GetEnqueueException ( "Failed to enqueue the operation" ) ) ;
186187 }
187188
188189 return taskCompletionSource . Task ;
@@ -212,7 +213,7 @@ public static Task<T> EnqueueAsync<T>(this DispatcherQueue dispatcher, Func<Task
212213 return awaitableResult ;
213214 }
214215
215- return Task . FromException < T > ( new InvalidOperationException ( "The Task returned by function cannot be null." ) ) ;
216+ return Task . FromException < T > ( GetEnqueueException ( "The Task returned by function cannot be null." ) ) ;
216217 }
217218 catch ( Exception e )
218219 {
@@ -236,7 +237,7 @@ static Task<T> TryEnqueueAsync(DispatcherQueue dispatcher, Func<Task<T>> functio
236237 }
237238 else
238239 {
239- taskCompletionSource . SetException ( new InvalidOperationException ( "The Task returned by function cannot be null." ) ) ;
240+ taskCompletionSource . SetException ( GetEnqueueException ( "The Task returned by function cannot be null." ) ) ;
240241 }
241242 }
242243 catch ( Exception e )
@@ -245,13 +246,24 @@ static Task<T> TryEnqueueAsync(DispatcherQueue dispatcher, Func<Task<T>> functio
245246 }
246247 } ) )
247248 {
248- taskCompletionSource . SetException ( new InvalidOperationException ( "Failed to enqueue the operation" ) ) ;
249+ taskCompletionSource . SetException ( GetEnqueueException ( "Failed to enqueue the operation" ) ) ;
249250 }
250251
251252 return taskCompletionSource . Task ;
252253 }
253254
254255 return TryEnqueueAsync ( dispatcher , function , priority ) ;
255256 }
257+
258+ /// <summary>
259+ /// Creates an <see cref="InvalidOperationException"/> to return when an enqueue operation fails.
260+ /// </summary>
261+ /// <param name="message">The message of the exception.</param>
262+ /// <returns>An <see cref="InvalidOperationException"/> with a specified message.</returns>
263+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
264+ private static InvalidOperationException GetEnqueueException ( string message )
265+ {
266+ return new InvalidOperationException ( message ) ;
267+ }
256268 }
257269}
0 commit comments