@@ -457,17 +457,16 @@ private static void ImmediateCancelBin()
457457 byte [ ] data = new byte [ dataSize ] ;
458458 rand . NextBytes ( data ) ;
459459 MemoryStream ms = new MemoryStream ( data , false ) ;
460- cmd . CommandText = "insert into #blobs (Id, blob) values (1, @blob)" ;
460+ // Include a delay to allow time for cancellation
461+ cmd . CommandText = "WAITFOR DELAY '00:00:05'; insert into #blobs (Id, blob) values (1, @blob)" ;
461462 cmd . Parameters . Add ( "@blob" , SqlDbType . VarBinary , dataSize ) ;
462463 cmd . Parameters [ "@blob" ] . Direction = ParameterDirection . Input ;
463464 cmd . Parameters [ "@blob" ] . Value = ms ;
464- Task t = cmd . ExecuteNonQueryAsync ( cts . Token ) ;
465- if ( ! t . IsCompleted )
466- cts . Cancel ( ) ;
465+
467466 try
468467 {
469- t . Wait ( ) ;
470- Console . WriteLine ( "FAIL: Expected AggregateException on Task wait for Cancelled Task! Task Status: " + t . Status ) ;
468+ Task . WaitAll ( cmd . ExecuteNonQueryAsync ( cts . Token ) , Task . Run ( ( ) => cts . Cancel ( ) ) ) ;
469+ Console . WriteLine ( "FAIL: Expected AggregateException on Task wait for Cancelled Task!" ) ;
471470 }
472471 catch ( AggregateException ae )
473472 {
@@ -504,17 +503,16 @@ private static void ImmediateCancelText()
504503 StringBuilder sb = new StringBuilder ( ) ;
505504 for ( int i = 0 ; i < 1000000 ; i ++ )
506505 sb . Append ( i ) ;
507- cmd . CommandText = "insert into #blobs (Id, blob) values (1, @blob)" ;
506+ // Include a delay to allow time for cancellation
507+ cmd . CommandText = "WAITFOR DELAY '00:00:05'; insert into #blobs (Id, blob) values (1, @blob)" ;
508508 cmd . Parameters . Add ( "@blob" , SqlDbType . VarChar , - 1 ) ;
509509 cmd . Parameters [ "@blob" ] . Direction = ParameterDirection . Input ;
510510 cmd . Parameters [ "@blob" ] . Value = new StringReader ( sb . ToString ( ) ) ;
511- Task t = cmd . ExecuteNonQueryAsync ( cts . Token ) ;
512- if ( ! t . IsCompleted )
513- cts . Cancel ( ) ;
511+
514512 try
515513 {
516- t . Wait ( ) ;
517- Console . WriteLine ( "FAIL: Expected AggregateException on Task wait for Cancelled Task! Task Status: " + t . Status ) ;
514+ Task . WaitAll ( cmd . ExecuteNonQueryAsync ( cts . Token ) , Task . Run ( ( ) => cts . Cancel ( ) ) ) ;
515+ Console . WriteLine ( "FAIL: Expected AggregateException on Task wait for Cancelled Task!" ) ;
518516 }
519517 catch ( AggregateException ae )
520518 {
@@ -544,17 +542,16 @@ private static void ImmediateCancelXml()
544542 {
545543 cmd . CommandText = "create table #blobs (Id int, blob xml)" ;
546544 cmd . ExecuteNonQuery ( ) ;
547- cmd . CommandText = "insert into #blobs (Id, blob) values (1, @blob)" ;
545+ // Include a delay to allow time for cancellation
546+ cmd . CommandText = "WAITFOR DELAY '00:00:05'; insert into #blobs (Id, blob) values (1, @blob)" ;
548547 cmd . Parameters . Add ( "@blob" , SqlDbType . Xml , - 1 ) ;
549548 cmd . Parameters [ "@blob" ] . Direction = ParameterDirection . Input ;
550549 cmd . Parameters [ "@blob" ] . Value = XmlReader . Create ( new StringReader ( XmlStr ) ) ;
551- Task t = cmd . ExecuteNonQueryAsync ( cts . Token ) ;
552- if ( ! t . IsCompleted )
553- cts . Cancel ( ) ;
550+
554551 try
555552 {
556- t . Wait ( ) ;
557- Console . WriteLine ( "FAIL: Expected AggregateException on Task wait for Cancelled Task! Task Status: " + t . Status ) ;
553+ Task . WaitAll ( cmd . ExecuteNonQueryAsync ( cts . Token ) , Task . Run ( ( ) => cts . Cancel ( ) ) ) ;
554+ Console . WriteLine ( "FAIL: Expected AggregateException on Task wait for Cancelled Task!" ) ;
558555 }
559556 catch ( AggregateException ae )
560557 {
0 commit comments