@@ -24,19 +24,18 @@ public class VisualUITestBase
2424 /// </summary>
2525 /// <param name="content">Content to set in test app.</param>
2626 /// <returns>When UI is loaded.</returns>
27- protected Task < bool > SetTestContentAsync ( FrameworkElement content , TaskCreationOptions ? options = null )
27+ protected Task SetTestContentAsync ( FrameworkElement content )
2828 {
29- var taskCompletionSource = options . HasValue ? new TaskCompletionSource < bool > ( options . Value )
30- : new TaskCompletionSource < bool > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
31-
32- App . DispatcherQueue . EnqueueAsync ( ( ) =>
29+ return App . DispatcherQueue . EnqueueAsync ( ( ) =>
3330 {
31+ var taskCompletionSource = new TaskCompletionSource < bool > ( ) ;
32+
3433 async void Callback ( object sender , RoutedEventArgs args )
3534 {
3635 content . Loaded -= Callback ;
3736
3837 // Wait for first Render pass
39- await CompositionTargetHelper . ExecuteAfterCompositionRenderingAsync ( ( ) => { } , TaskCreationOptions . RunContinuationsAsynchronously ) ;
38+ await CompositionTargetHelper . ExecuteAfterCompositionRenderingAsync ( ( ) => { } ) ;
4039
4140 taskCompletionSource . SetResult ( true ) ;
4241 }
@@ -53,35 +52,26 @@ async void Callback(object sender, RoutedEventArgs args)
5352 {
5453 taskCompletionSource . SetException ( e ) ;
5554 }
56- } ) ;
5755
58- return taskCompletionSource . Task ;
56+ return taskCompletionSource . Task ;
57+ } ) ;
5958 }
6059
6160 [ TestCleanup ]
6261 public async Task Cleanup ( )
6362 {
6463 var taskCompletionSource = new TaskCompletionSource < bool > ( ) ;
6564
66- // Need to await TaskCompletionSource before Task
67- // See https://devblogs.microsoft.com/premier-developer/the-danger-of-taskcompletionsourcet-class/
68- await taskCompletionSource . Task ;
69-
7065 await App . DispatcherQueue . EnqueueAsync ( ( ) =>
7166 {
72- void Callback ( object sender , RoutedEventArgs args )
73- {
74- App . ContentRoot . Unloaded -= Callback ;
75-
76- taskCompletionSource . SetResult ( true ) ;
77- }
78-
7967 // Going to wait for our original content to unload
80- App . ContentRoot . Unloaded += Callback ;
68+ App . ContentRoot . Unloaded += ( _ , _ ) => taskCompletionSource . SetResult ( true ) ;
8169
8270 // Trigger that now
8371 App . ContentRoot = null ;
8472 } ) ;
73+
74+ await taskCompletionSource . Task ;
8575 }
8676 }
8777}
0 commit comments