Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 04e3885

Browse files
committed
Merge pull request #1954 from Maxwe11/console-improvement
Eliminate closure allocation in Console.HandleBreakEvent
2 parents 816cccf + fb71dcc commit 04e3885

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/System.Console/src/System/Console.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,6 @@ public static void ResetColor()
9090
ConsolePal.ResetColor();
9191
}
9292

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-
10593
public static event ConsoleCancelEventHandler CancelKeyPress
10694
{
10795
add
@@ -415,16 +403,27 @@ public static void Write(String value)
415403

416404
private sealed class ControlCDelegateData
417405
{
418-
internal readonly ConsoleSpecialKey ControlKey;
419-
internal readonly ConsoleCancelEventHandler CancelCallbacks;
406+
private readonly ConsoleSpecialKey _controlKey;
407+
private readonly ConsoleCancelEventHandler _cancelCallbacks;
420408

421409
internal bool Cancel;
422410
internal bool DelegateStarted;
423411

424412
internal ControlCDelegateData(ConsoleSpecialKey controlKey, ConsoleCancelEventHandler cancelCallbacks)
425413
{
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;
428427
}
429428
}
430429

@@ -441,12 +440,13 @@ internal static bool HandleBreakEvent(ConsoleSpecialKey controlKey)
441440
return false;
442441
}
443442

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);
450450

451451
// Block until the delegate is done. We need to be robust in the face of the task not executing
452452
// but we also want to get control back immediately after it is done and we don't want to give the

0 commit comments

Comments
 (0)