@@ -527,31 +527,39 @@ public void Wakeup ()
527527 // TODO: Add IMainLoop tests
528528
529529 volatile static int tbCounter = 0 ;
530+ static ManualResetEventSlim _wakeUp = new ManualResetEventSlim ( false ) ;
530531
531- private static void Launch ( Random r , TextField tf )
532+ private static void Launch ( Random r , TextField tf , int target )
532533 {
533534 Task . Run ( ( ) => {
534535 Thread . Sleep ( r . Next ( 2 , 4 ) ) ;
535536 Application . MainLoop . Invoke ( ( ) => {
536537 tf . Text = $ "index{ r . Next ( ) } ";
537538 Interlocked . Increment ( ref tbCounter ) ;
539+ if ( target == tbCounter ) {
540+ // On last increment wake up the check
541+ _wakeUp . Set ( ) ;
542+ }
538543 } ) ;
539544 } ) ;
540545 }
541546
542547 private static void RunTest ( Random r , TextField tf , int numPasses , int numIncrements , int pollMs )
543548 {
544549 for ( int j = 0 ; j < numPasses ; j ++ ) {
550+
551+ _wakeUp . Reset ( ) ;
545552 for ( var i = 0 ; i < numIncrements ; i ++ ) {
546- Launch ( r , tf ) ;
553+ Launch ( r , tf , ( j + 1 ) * numIncrements ) ;
547554 }
548555
556+
549557 while ( tbCounter != ( j + 1 ) * numIncrements ) // Wait for tbCounter to reach expected value
550558 {
551559 var tbNow = tbCounter ;
552- Thread . Sleep ( pollMs ) ;
560+ _wakeUp . Wait ( pollMs ) ;
553561 if ( tbCounter == tbNow ) {
554- // No change after sleep : Idle handlers added via Application.MainLoop.Invoke have gone missing
562+ // No change after wait : Idle handlers added via Application.MainLoop.Invoke have gone missing
555563 Application . MainLoop . Invoke ( ( ) => Application . RequestStop ( ) ) ;
556564 throw new TimeoutException (
557565 $ "Timeout: Increment lost. tbCounter ({ tbCounter } ) didn't " +
@@ -572,7 +580,7 @@ public async Task InvokeLeakTest ()
572580
573581 const int numPasses = 10 ;
574582 const int numIncrements = 10000 ;
575- const int pollMs = 500 ;
583+ const int pollMs = 20000 ;
576584
577585 var task = Task . Run ( ( ) => RunTest ( r , tf , numPasses , numIncrements , pollMs ) ) ;
578586
0 commit comments