@@ -90,18 +90,6 @@ public static void ResetColor()
90
90
ConsolePal . ResetColor ( ) ;
91
91
}
92
92
93
- // This is the worker delegate that is called on the Threadpool thread to fire the actual events. It sets the DelegateStarted flag so
94
- // the thread that queued the work to the threadpool knows it has started (since it does not want to block indefinitely on the task
95
- // to start).
96
- private static void ControlCDelegate ( object data )
97
- {
98
- ControlCDelegateData controlCData = ( ControlCDelegateData ) data ;
99
- controlCData . DelegateStarted = true ;
100
- ConsoleCancelEventArgs args = new ConsoleCancelEventArgs ( controlCData . ControlKey ) ;
101
- controlCData . CancelCallbacks ( null , args ) ;
102
- controlCData . Cancel = args . Cancel ;
103
- }
104
-
105
93
public static event ConsoleCancelEventHandler CancelKeyPress
106
94
{
107
95
add
@@ -415,16 +403,27 @@ public static void Write(String value)
415
403
416
404
private sealed class ControlCDelegateData
417
405
{
418
- internal readonly ConsoleSpecialKey ControlKey ;
419
- internal readonly ConsoleCancelEventHandler CancelCallbacks ;
406
+ private readonly ConsoleSpecialKey _controlKey ;
407
+ private readonly ConsoleCancelEventHandler _cancelCallbacks ;
420
408
421
409
internal bool Cancel ;
422
410
internal bool DelegateStarted ;
423
411
424
412
internal ControlCDelegateData ( ConsoleSpecialKey controlKey , ConsoleCancelEventHandler cancelCallbacks )
425
413
{
426
- this . ControlKey = controlKey ;
427
- this . CancelCallbacks = cancelCallbacks ;
414
+ _controlKey = controlKey ;
415
+ _cancelCallbacks = cancelCallbacks ;
416
+ }
417
+
418
+ // This is the worker delegate that is called on the Threadpool thread to fire the actual events. It sets the DelegateStarted flag so
419
+ // the thread that queued the work to the threadpool knows it has started (since it does not want to block indefinitely on the task
420
+ // to start).
421
+ internal void HandleBreakEvent ( )
422
+ {
423
+ DelegateStarted = true ;
424
+ var args = new ConsoleCancelEventArgs ( _controlKey ) ;
425
+ _cancelCallbacks ( null , args ) ;
426
+ Cancel = args . Cancel ;
428
427
}
429
428
}
430
429
@@ -441,12 +440,13 @@ internal static bool HandleBreakEvent(ConsoleSpecialKey controlKey)
441
440
return false ;
442
441
}
443
442
444
- ControlCDelegateData delegateData = new ControlCDelegateData ( controlKey , cancelCallbacks ) ;
445
-
446
- Task callBackTask = Task . Run ( ( ) =>
447
- {
448
- ControlCDelegate ( delegateData ) ;
449
- } ) ;
443
+ var delegateData = new ControlCDelegateData ( controlKey , cancelCallbacks ) ;
444
+ Task callBackTask = Task . Factory . StartNew (
445
+ d => ( ( ControlCDelegateData ) d ) . HandleBreakEvent ( ) ,
446
+ delegateData ,
447
+ CancellationToken . None ,
448
+ TaskCreationOptions . DenyChildAttach ,
449
+ TaskScheduler . Default ) ;
450
450
451
451
// Block until the delegate is done. We need to be robust in the face of the task not executing
452
452
// but we also want to get control back immediately after it is done and we don't want to give the
0 commit comments