how to eliminate extraneous console output after ctrl-c in Windows?How can I develop for iPhone using a Windows development machine?How can you find out which process is listening on a port on Windows?Is there a better Windows Console Window?How can I update the current line in a C# Windows Console App?How to prevent auto-closing of console after the execution of batch fileRun cURL commands from Windows consoleHow do I install pip on Windows?How do I run Redis on Windows?How do I run two commands in one line in Windows CMD?How can I update npm on Windows?

What was the deeper meaning of Hermione wanting the cloak?

Manager manipulates my leaves, what's in it for him?

Which museums have artworks of all four ninja turtles' namesakes?

What are sources for Magic Items that are not adventure-specific?

I reverse the source code, you negate the output!

Should the pagination be reset when changing the order?

Intuitive methods for representation of Cartesian Coordinates in terms of Spherical Coordinates as basis

What did the controller say during my approach to land (audio clip)?

Is there any reason nowadays to use a neon indicator lamp instead of a LED?

As an employer, can I compel my employees to vote?

What is the word for a person who destroys monuments?

Convert dictionaries with list of values into a dataframe

Who are the people reviewing far more papers than they're submitting for review?

Why are two-stroke engines nearly unheard of in aviation?

Is Zack Morris's 'time stop' ability in "Saved By the Bell" a supernatural ability?

Why do things cool down?

Is it safe to unplug a blinking USB drive after 'safely' ejecting it?

Why is the stock market so unpredictable?

Floating Point XOR

Is there any actual security benefit to restricting foreign IPs?

Do household ovens ventilate heat to the outdoors?

Did slaves have slaves?

Lead Amalgam as a Material for a Sword

Do the villains know Batman has no superpowers?



how to eliminate extraneous console output after ctrl-c in Windows?


How can I develop for iPhone using a Windows development machine?How can you find out which process is listening on a port on Windows?Is there a better Windows Console Window?How can I update the current line in a C# Windows Console App?How to prevent auto-closing of console after the execution of batch fileRun cURL commands from Windows consoleHow do I install pip on Windows?How do I run Redis on Windows?How do I run two commands in one line in Windows CMD?How can I update npm on Windows?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















When pressing Ctrl-c there is almost always additional output. I'd like to ensure that after receiving the Ctrl-c the program doesn't show anything other than possibly "^C".



I found what is mostly the same question but, it was for Linux and my attempts to "port" the solution from Linux to Windows have not succeeded.



At this point, I'm out of things to try and can use some help, which I will definitely appreciate. Thank you.



The short example program below suffers from that problem.



$APPTYPE CONSOLE

program _SetConsoleCtrlHandler;

uses
Windows,
SysUtils
;

function CtrlHandler(CtrlType : DWORD) : BOOL; stdcall;
begin
result := FALSE;

case CtrlType of
CTRL_C_EVENT,
CTRL_BREAK_EVENT:
begin
result := TRUE;

ExitProcess(7);
end;
end;
end;

var
s : shortstring;

begin
SetConsoleCtrlHandler(@CtrlHandler, TRUE);

while TRUE do
begin
write('press <ctrl-c> to end this program : ');
readln(s);
end;
end.









share|improve this question
























  • Besides my answer below, you shouldn't be looping after the readln. You are either creating a single threaded app that executes from top to bottom until you detect Ctrl-C and immediately exit or an app where all other work is done in a thread and you are waiting for Ctrl-C in the main app thread.

    – Allen Drennan
    Mar 28 at 14:50







  • 2





    CtrlHandler runs in a different thread

    – Anders
    Mar 28 at 14:57






  • 1





    Your problem identification does not seem to be correct. E.g. empty your loop (while true do;) then press ctrl+c and see if there's any extraneous output.

    – Sertac Akyuz
    Mar 28 at 15:03







  • 1





    @ScienceAmatuer That's not really a reliable approach because you cannot guarantee the timing of the signal that Ctrl-C was pressed. Like I said below, you really need to structure your exit process to wait on the ctrl-c.

    – Allen Drennan
    Mar 28 at 15:25






  • 1





    @ Allen, the application cannot be waiting for a ctrl-c. It has to process user input and to do that it has to display an appropriate prompt and readln whatever the user typed.

    – ScienceAmateur
    Mar 28 at 15:36

















2















When pressing Ctrl-c there is almost always additional output. I'd like to ensure that after receiving the Ctrl-c the program doesn't show anything other than possibly "^C".



I found what is mostly the same question but, it was for Linux and my attempts to "port" the solution from Linux to Windows have not succeeded.



At this point, I'm out of things to try and can use some help, which I will definitely appreciate. Thank you.



The short example program below suffers from that problem.



$APPTYPE CONSOLE

program _SetConsoleCtrlHandler;

uses
Windows,
SysUtils
;

function CtrlHandler(CtrlType : DWORD) : BOOL; stdcall;
begin
result := FALSE;

case CtrlType of
CTRL_C_EVENT,
CTRL_BREAK_EVENT:
begin
result := TRUE;

ExitProcess(7);
end;
end;
end;

var
s : shortstring;

begin
SetConsoleCtrlHandler(@CtrlHandler, TRUE);

while TRUE do
begin
write('press <ctrl-c> to end this program : ');
readln(s);
end;
end.









share|improve this question
























  • Besides my answer below, you shouldn't be looping after the readln. You are either creating a single threaded app that executes from top to bottom until you detect Ctrl-C and immediately exit or an app where all other work is done in a thread and you are waiting for Ctrl-C in the main app thread.

    – Allen Drennan
    Mar 28 at 14:50







  • 2





    CtrlHandler runs in a different thread

    – Anders
    Mar 28 at 14:57






  • 1





    Your problem identification does not seem to be correct. E.g. empty your loop (while true do;) then press ctrl+c and see if there's any extraneous output.

    – Sertac Akyuz
    Mar 28 at 15:03







  • 1





    @ScienceAmatuer That's not really a reliable approach because you cannot guarantee the timing of the signal that Ctrl-C was pressed. Like I said below, you really need to structure your exit process to wait on the ctrl-c.

    – Allen Drennan
    Mar 28 at 15:25






  • 1





    @ Allen, the application cannot be waiting for a ctrl-c. It has to process user input and to do that it has to display an appropriate prompt and readln whatever the user typed.

    – ScienceAmateur
    Mar 28 at 15:36













2












2








2








When pressing Ctrl-c there is almost always additional output. I'd like to ensure that after receiving the Ctrl-c the program doesn't show anything other than possibly "^C".



I found what is mostly the same question but, it was for Linux and my attempts to "port" the solution from Linux to Windows have not succeeded.



At this point, I'm out of things to try and can use some help, which I will definitely appreciate. Thank you.



The short example program below suffers from that problem.



$APPTYPE CONSOLE

program _SetConsoleCtrlHandler;

uses
Windows,
SysUtils
;

function CtrlHandler(CtrlType : DWORD) : BOOL; stdcall;
begin
result := FALSE;

case CtrlType of
CTRL_C_EVENT,
CTRL_BREAK_EVENT:
begin
result := TRUE;

ExitProcess(7);
end;
end;
end;

var
s : shortstring;

begin
SetConsoleCtrlHandler(@CtrlHandler, TRUE);

while TRUE do
begin
write('press <ctrl-c> to end this program : ');
readln(s);
end;
end.









share|improve this question














When pressing Ctrl-c there is almost always additional output. I'd like to ensure that after receiving the Ctrl-c the program doesn't show anything other than possibly "^C".



I found what is mostly the same question but, it was for Linux and my attempts to "port" the solution from Linux to Windows have not succeeded.



At this point, I'm out of things to try and can use some help, which I will definitely appreciate. Thank you.



The short example program below suffers from that problem.



$APPTYPE CONSOLE

program _SetConsoleCtrlHandler;

uses
Windows,
SysUtils
;

function CtrlHandler(CtrlType : DWORD) : BOOL; stdcall;
begin
result := FALSE;

case CtrlType of
CTRL_C_EVENT,
CTRL_BREAK_EVENT:
begin
result := TRUE;

ExitProcess(7);
end;
end;
end;

var
s : shortstring;

begin
SetConsoleCtrlHandler(@CtrlHandler, TRUE);

while TRUE do
begin
write('press <ctrl-c> to end this program : ');
readln(s);
end;
end.






windows delphi winapi windows-console






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 28 at 14:14









ScienceAmateurScienceAmateur

18410 bronze badges




18410 bronze badges















  • Besides my answer below, you shouldn't be looping after the readln. You are either creating a single threaded app that executes from top to bottom until you detect Ctrl-C and immediately exit or an app where all other work is done in a thread and you are waiting for Ctrl-C in the main app thread.

    – Allen Drennan
    Mar 28 at 14:50







  • 2





    CtrlHandler runs in a different thread

    – Anders
    Mar 28 at 14:57






  • 1





    Your problem identification does not seem to be correct. E.g. empty your loop (while true do;) then press ctrl+c and see if there's any extraneous output.

    – Sertac Akyuz
    Mar 28 at 15:03







  • 1





    @ScienceAmatuer That's not really a reliable approach because you cannot guarantee the timing of the signal that Ctrl-C was pressed. Like I said below, you really need to structure your exit process to wait on the ctrl-c.

    – Allen Drennan
    Mar 28 at 15:25






  • 1





    @ Allen, the application cannot be waiting for a ctrl-c. It has to process user input and to do that it has to display an appropriate prompt and readln whatever the user typed.

    – ScienceAmateur
    Mar 28 at 15:36

















  • Besides my answer below, you shouldn't be looping after the readln. You are either creating a single threaded app that executes from top to bottom until you detect Ctrl-C and immediately exit or an app where all other work is done in a thread and you are waiting for Ctrl-C in the main app thread.

    – Allen Drennan
    Mar 28 at 14:50







  • 2





    CtrlHandler runs in a different thread

    – Anders
    Mar 28 at 14:57






  • 1





    Your problem identification does not seem to be correct. E.g. empty your loop (while true do;) then press ctrl+c and see if there's any extraneous output.

    – Sertac Akyuz
    Mar 28 at 15:03







  • 1





    @ScienceAmatuer That's not really a reliable approach because you cannot guarantee the timing of the signal that Ctrl-C was pressed. Like I said below, you really need to structure your exit process to wait on the ctrl-c.

    – Allen Drennan
    Mar 28 at 15:25






  • 1





    @ Allen, the application cannot be waiting for a ctrl-c. It has to process user input and to do that it has to display an appropriate prompt and readln whatever the user typed.

    – ScienceAmateur
    Mar 28 at 15:36
















Besides my answer below, you shouldn't be looping after the readln. You are either creating a single threaded app that executes from top to bottom until you detect Ctrl-C and immediately exit or an app where all other work is done in a thread and you are waiting for Ctrl-C in the main app thread.

– Allen Drennan
Mar 28 at 14:50






Besides my answer below, you shouldn't be looping after the readln. You are either creating a single threaded app that executes from top to bottom until you detect Ctrl-C and immediately exit or an app where all other work is done in a thread and you are waiting for Ctrl-C in the main app thread.

– Allen Drennan
Mar 28 at 14:50





2




2





CtrlHandler runs in a different thread

– Anders
Mar 28 at 14:57





CtrlHandler runs in a different thread

– Anders
Mar 28 at 14:57




1




1





Your problem identification does not seem to be correct. E.g. empty your loop (while true do;) then press ctrl+c and see if there's any extraneous output.

– Sertac Akyuz
Mar 28 at 15:03






Your problem identification does not seem to be correct. E.g. empty your loop (while true do;) then press ctrl+c and see if there's any extraneous output.

– Sertac Akyuz
Mar 28 at 15:03





1




1





@ScienceAmatuer That's not really a reliable approach because you cannot guarantee the timing of the signal that Ctrl-C was pressed. Like I said below, you really need to structure your exit process to wait on the ctrl-c.

– Allen Drennan
Mar 28 at 15:25





@ScienceAmatuer That's not really a reliable approach because you cannot guarantee the timing of the signal that Ctrl-C was pressed. Like I said below, you really need to structure your exit process to wait on the ctrl-c.

– Allen Drennan
Mar 28 at 15:25




1




1





@ Allen, the application cannot be waiting for a ctrl-c. It has to process user input and to do that it has to display an appropriate prompt and readln whatever the user typed.

– ScienceAmateur
Mar 28 at 15:36





@ Allen, the application cannot be waiting for a ctrl-c. It has to process user input and to do that it has to display an appropriate prompt and readln whatever the user typed.

– ScienceAmateur
Mar 28 at 15:36












1 Answer
1






active

oldest

votes


















2
















The way I usually do this is to have a separate unit that is signaled and a simple wait, like the following. In the main console project you call WaitForCtrlC instead of Readln(). You could also use a TEvent and wait on the event instead of looping, like I show in this example:



uses
$IFDEF LINUX
Posix.Signal,
$ENDIF
$IFDEF MSWINDOWS
Windows,
$ENDIF
SysUtils;

procedure WaitForCtrlC;

implementation

var
Control_C: Boolean = False;

$IFDEF MSWINDOWS
function ConsoleCtrlHandler(dwCtrlType: DWORD): BOOL; stdcall;
begin
if (dwCtrlType = CTRL_C_EVENT) then
Control_C := True;
Result := True;
end;
$ENDIF

$IFDEF LINUX
var
sigIntHandler: sigaction_t;

procedure SigHandler(SigNum: Integer); cdecl;
begin
Control_C := True;
end;
$ENDIF

procedure WaitForCtrlC;
begin
while not Control_C do
Sleep(25);
end;

initialization
$IFDEF MSWINDOWS
Windows.SetConsoleCtrlHandler(@ConsoleCtrlHandler, True);
$ENDIF
$IFDEF LINUX
sigIntHandler._u.sa_handler := @SigHandler;
sigemptyset(sigIntHandler.sa_mask);
sigIntHandler.sa_flags := 0;
sigaction(SIGINT, @sigIntHandler, nil);
$ENDIF





share|improve this answer

























  • @ Allen, thank you for your help. The problem isn't the readln, Adding a Sleep(5) - or even Sleep (1) - after the readln, solves the problem in the sample code I posted. It seems that once ctrl-c has been pressed, there is a race condition. Without the Sleep(xx) the loop has enough time to execute again before the call to ExitProcess is made. Just FYI, I up voted your answer but, I won't check it as the answer because the real answer is to somehow (usually with a call to sleep) give enough time for the control handler to execute the call to ExitProcess/TerminateProcess..

    – ScienceAmateur
    Mar 28 at 15:14











  • I think the main point here is you shouldn't be using Readln() if your intent is to shutdown your app with Ctrl-C. If you do it by waiting for Ctrl-C the problem you describe goes away.

    – Allen Drennan
    Mar 28 at 15:19











  • @ Allen, the readln is necessary. In the typical command line interpreter there is the combination write <prompt> followed by readln <command>. There is nothing wrong with using writeln and readln. The problem is to ensure that the ctrl-c handler always wins the execute race by introducing a delay in the loop.

    – ScienceAmateur
    Mar 28 at 15:23






  • 1





    @ScienceAmateur Neither your question nor your example discuss that intent at all. Your example only shows you wanting to exit your program when you press Ctlr-C.

    – Allen Drennan
    Mar 28 at 15:29











  • @ Allen, I made the example as simple as possible for illustrative purposes. I might have made it a bit too simple.

    – ScienceAmateur
    Mar 28 at 15:39










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55399769%2fhow-to-eliminate-extraneous-console-output-after-ctrl-c-in-windows%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2
















The way I usually do this is to have a separate unit that is signaled and a simple wait, like the following. In the main console project you call WaitForCtrlC instead of Readln(). You could also use a TEvent and wait on the event instead of looping, like I show in this example:



uses
$IFDEF LINUX
Posix.Signal,
$ENDIF
$IFDEF MSWINDOWS
Windows,
$ENDIF
SysUtils;

procedure WaitForCtrlC;

implementation

var
Control_C: Boolean = False;

$IFDEF MSWINDOWS
function ConsoleCtrlHandler(dwCtrlType: DWORD): BOOL; stdcall;
begin
if (dwCtrlType = CTRL_C_EVENT) then
Control_C := True;
Result := True;
end;
$ENDIF

$IFDEF LINUX
var
sigIntHandler: sigaction_t;

procedure SigHandler(SigNum: Integer); cdecl;
begin
Control_C := True;
end;
$ENDIF

procedure WaitForCtrlC;
begin
while not Control_C do
Sleep(25);
end;

initialization
$IFDEF MSWINDOWS
Windows.SetConsoleCtrlHandler(@ConsoleCtrlHandler, True);
$ENDIF
$IFDEF LINUX
sigIntHandler._u.sa_handler := @SigHandler;
sigemptyset(sigIntHandler.sa_mask);
sigIntHandler.sa_flags := 0;
sigaction(SIGINT, @sigIntHandler, nil);
$ENDIF





share|improve this answer

























  • @ Allen, thank you for your help. The problem isn't the readln, Adding a Sleep(5) - or even Sleep (1) - after the readln, solves the problem in the sample code I posted. It seems that once ctrl-c has been pressed, there is a race condition. Without the Sleep(xx) the loop has enough time to execute again before the call to ExitProcess is made. Just FYI, I up voted your answer but, I won't check it as the answer because the real answer is to somehow (usually with a call to sleep) give enough time for the control handler to execute the call to ExitProcess/TerminateProcess..

    – ScienceAmateur
    Mar 28 at 15:14











  • I think the main point here is you shouldn't be using Readln() if your intent is to shutdown your app with Ctrl-C. If you do it by waiting for Ctrl-C the problem you describe goes away.

    – Allen Drennan
    Mar 28 at 15:19











  • @ Allen, the readln is necessary. In the typical command line interpreter there is the combination write <prompt> followed by readln <command>. There is nothing wrong with using writeln and readln. The problem is to ensure that the ctrl-c handler always wins the execute race by introducing a delay in the loop.

    – ScienceAmateur
    Mar 28 at 15:23






  • 1





    @ScienceAmateur Neither your question nor your example discuss that intent at all. Your example only shows you wanting to exit your program when you press Ctlr-C.

    – Allen Drennan
    Mar 28 at 15:29











  • @ Allen, I made the example as simple as possible for illustrative purposes. I might have made it a bit too simple.

    – ScienceAmateur
    Mar 28 at 15:39















2
















The way I usually do this is to have a separate unit that is signaled and a simple wait, like the following. In the main console project you call WaitForCtrlC instead of Readln(). You could also use a TEvent and wait on the event instead of looping, like I show in this example:



uses
$IFDEF LINUX
Posix.Signal,
$ENDIF
$IFDEF MSWINDOWS
Windows,
$ENDIF
SysUtils;

procedure WaitForCtrlC;

implementation

var
Control_C: Boolean = False;

$IFDEF MSWINDOWS
function ConsoleCtrlHandler(dwCtrlType: DWORD): BOOL; stdcall;
begin
if (dwCtrlType = CTRL_C_EVENT) then
Control_C := True;
Result := True;
end;
$ENDIF

$IFDEF LINUX
var
sigIntHandler: sigaction_t;

procedure SigHandler(SigNum: Integer); cdecl;
begin
Control_C := True;
end;
$ENDIF

procedure WaitForCtrlC;
begin
while not Control_C do
Sleep(25);
end;

initialization
$IFDEF MSWINDOWS
Windows.SetConsoleCtrlHandler(@ConsoleCtrlHandler, True);
$ENDIF
$IFDEF LINUX
sigIntHandler._u.sa_handler := @SigHandler;
sigemptyset(sigIntHandler.sa_mask);
sigIntHandler.sa_flags := 0;
sigaction(SIGINT, @sigIntHandler, nil);
$ENDIF





share|improve this answer

























  • @ Allen, thank you for your help. The problem isn't the readln, Adding a Sleep(5) - or even Sleep (1) - after the readln, solves the problem in the sample code I posted. It seems that once ctrl-c has been pressed, there is a race condition. Without the Sleep(xx) the loop has enough time to execute again before the call to ExitProcess is made. Just FYI, I up voted your answer but, I won't check it as the answer because the real answer is to somehow (usually with a call to sleep) give enough time for the control handler to execute the call to ExitProcess/TerminateProcess..

    – ScienceAmateur
    Mar 28 at 15:14











  • I think the main point here is you shouldn't be using Readln() if your intent is to shutdown your app with Ctrl-C. If you do it by waiting for Ctrl-C the problem you describe goes away.

    – Allen Drennan
    Mar 28 at 15:19











  • @ Allen, the readln is necessary. In the typical command line interpreter there is the combination write <prompt> followed by readln <command>. There is nothing wrong with using writeln and readln. The problem is to ensure that the ctrl-c handler always wins the execute race by introducing a delay in the loop.

    – ScienceAmateur
    Mar 28 at 15:23






  • 1





    @ScienceAmateur Neither your question nor your example discuss that intent at all. Your example only shows you wanting to exit your program when you press Ctlr-C.

    – Allen Drennan
    Mar 28 at 15:29











  • @ Allen, I made the example as simple as possible for illustrative purposes. I might have made it a bit too simple.

    – ScienceAmateur
    Mar 28 at 15:39













2














2










2









The way I usually do this is to have a separate unit that is signaled and a simple wait, like the following. In the main console project you call WaitForCtrlC instead of Readln(). You could also use a TEvent and wait on the event instead of looping, like I show in this example:



uses
$IFDEF LINUX
Posix.Signal,
$ENDIF
$IFDEF MSWINDOWS
Windows,
$ENDIF
SysUtils;

procedure WaitForCtrlC;

implementation

var
Control_C: Boolean = False;

$IFDEF MSWINDOWS
function ConsoleCtrlHandler(dwCtrlType: DWORD): BOOL; stdcall;
begin
if (dwCtrlType = CTRL_C_EVENT) then
Control_C := True;
Result := True;
end;
$ENDIF

$IFDEF LINUX
var
sigIntHandler: sigaction_t;

procedure SigHandler(SigNum: Integer); cdecl;
begin
Control_C := True;
end;
$ENDIF

procedure WaitForCtrlC;
begin
while not Control_C do
Sleep(25);
end;

initialization
$IFDEF MSWINDOWS
Windows.SetConsoleCtrlHandler(@ConsoleCtrlHandler, True);
$ENDIF
$IFDEF LINUX
sigIntHandler._u.sa_handler := @SigHandler;
sigemptyset(sigIntHandler.sa_mask);
sigIntHandler.sa_flags := 0;
sigaction(SIGINT, @sigIntHandler, nil);
$ENDIF





share|improve this answer













The way I usually do this is to have a separate unit that is signaled and a simple wait, like the following. In the main console project you call WaitForCtrlC instead of Readln(). You could also use a TEvent and wait on the event instead of looping, like I show in this example:



uses
$IFDEF LINUX
Posix.Signal,
$ENDIF
$IFDEF MSWINDOWS
Windows,
$ENDIF
SysUtils;

procedure WaitForCtrlC;

implementation

var
Control_C: Boolean = False;

$IFDEF MSWINDOWS
function ConsoleCtrlHandler(dwCtrlType: DWORD): BOOL; stdcall;
begin
if (dwCtrlType = CTRL_C_EVENT) then
Control_C := True;
Result := True;
end;
$ENDIF

$IFDEF LINUX
var
sigIntHandler: sigaction_t;

procedure SigHandler(SigNum: Integer); cdecl;
begin
Control_C := True;
end;
$ENDIF

procedure WaitForCtrlC;
begin
while not Control_C do
Sleep(25);
end;

initialization
$IFDEF MSWINDOWS
Windows.SetConsoleCtrlHandler(@ConsoleCtrlHandler, True);
$ENDIF
$IFDEF LINUX
sigIntHandler._u.sa_handler := @SigHandler;
sigemptyset(sigIntHandler.sa_mask);
sigIntHandler.sa_flags := 0;
sigaction(SIGINT, @sigIntHandler, nil);
$ENDIF






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 28 at 14:43









Allen DrennanAllen Drennan

2713 silver badges6 bronze badges




2713 silver badges6 bronze badges















  • @ Allen, thank you for your help. The problem isn't the readln, Adding a Sleep(5) - or even Sleep (1) - after the readln, solves the problem in the sample code I posted. It seems that once ctrl-c has been pressed, there is a race condition. Without the Sleep(xx) the loop has enough time to execute again before the call to ExitProcess is made. Just FYI, I up voted your answer but, I won't check it as the answer because the real answer is to somehow (usually with a call to sleep) give enough time for the control handler to execute the call to ExitProcess/TerminateProcess..

    – ScienceAmateur
    Mar 28 at 15:14











  • I think the main point here is you shouldn't be using Readln() if your intent is to shutdown your app with Ctrl-C. If you do it by waiting for Ctrl-C the problem you describe goes away.

    – Allen Drennan
    Mar 28 at 15:19











  • @ Allen, the readln is necessary. In the typical command line interpreter there is the combination write <prompt> followed by readln <command>. There is nothing wrong with using writeln and readln. The problem is to ensure that the ctrl-c handler always wins the execute race by introducing a delay in the loop.

    – ScienceAmateur
    Mar 28 at 15:23






  • 1





    @ScienceAmateur Neither your question nor your example discuss that intent at all. Your example only shows you wanting to exit your program when you press Ctlr-C.

    – Allen Drennan
    Mar 28 at 15:29











  • @ Allen, I made the example as simple as possible for illustrative purposes. I might have made it a bit too simple.

    – ScienceAmateur
    Mar 28 at 15:39

















  • @ Allen, thank you for your help. The problem isn't the readln, Adding a Sleep(5) - or even Sleep (1) - after the readln, solves the problem in the sample code I posted. It seems that once ctrl-c has been pressed, there is a race condition. Without the Sleep(xx) the loop has enough time to execute again before the call to ExitProcess is made. Just FYI, I up voted your answer but, I won't check it as the answer because the real answer is to somehow (usually with a call to sleep) give enough time for the control handler to execute the call to ExitProcess/TerminateProcess..

    – ScienceAmateur
    Mar 28 at 15:14











  • I think the main point here is you shouldn't be using Readln() if your intent is to shutdown your app with Ctrl-C. If you do it by waiting for Ctrl-C the problem you describe goes away.

    – Allen Drennan
    Mar 28 at 15:19











  • @ Allen, the readln is necessary. In the typical command line interpreter there is the combination write <prompt> followed by readln <command>. There is nothing wrong with using writeln and readln. The problem is to ensure that the ctrl-c handler always wins the execute race by introducing a delay in the loop.

    – ScienceAmateur
    Mar 28 at 15:23






  • 1





    @ScienceAmateur Neither your question nor your example discuss that intent at all. Your example only shows you wanting to exit your program when you press Ctlr-C.

    – Allen Drennan
    Mar 28 at 15:29











  • @ Allen, I made the example as simple as possible for illustrative purposes. I might have made it a bit too simple.

    – ScienceAmateur
    Mar 28 at 15:39
















@ Allen, thank you for your help. The problem isn't the readln, Adding a Sleep(5) - or even Sleep (1) - after the readln, solves the problem in the sample code I posted. It seems that once ctrl-c has been pressed, there is a race condition. Without the Sleep(xx) the loop has enough time to execute again before the call to ExitProcess is made. Just FYI, I up voted your answer but, I won't check it as the answer because the real answer is to somehow (usually with a call to sleep) give enough time for the control handler to execute the call to ExitProcess/TerminateProcess..

– ScienceAmateur
Mar 28 at 15:14





@ Allen, thank you for your help. The problem isn't the readln, Adding a Sleep(5) - or even Sleep (1) - after the readln, solves the problem in the sample code I posted. It seems that once ctrl-c has been pressed, there is a race condition. Without the Sleep(xx) the loop has enough time to execute again before the call to ExitProcess is made. Just FYI, I up voted your answer but, I won't check it as the answer because the real answer is to somehow (usually with a call to sleep) give enough time for the control handler to execute the call to ExitProcess/TerminateProcess..

– ScienceAmateur
Mar 28 at 15:14













I think the main point here is you shouldn't be using Readln() if your intent is to shutdown your app with Ctrl-C. If you do it by waiting for Ctrl-C the problem you describe goes away.

– Allen Drennan
Mar 28 at 15:19





I think the main point here is you shouldn't be using Readln() if your intent is to shutdown your app with Ctrl-C. If you do it by waiting for Ctrl-C the problem you describe goes away.

– Allen Drennan
Mar 28 at 15:19













@ Allen, the readln is necessary. In the typical command line interpreter there is the combination write <prompt> followed by readln <command>. There is nothing wrong with using writeln and readln. The problem is to ensure that the ctrl-c handler always wins the execute race by introducing a delay in the loop.

– ScienceAmateur
Mar 28 at 15:23





@ Allen, the readln is necessary. In the typical command line interpreter there is the combination write <prompt> followed by readln <command>. There is nothing wrong with using writeln and readln. The problem is to ensure that the ctrl-c handler always wins the execute race by introducing a delay in the loop.

– ScienceAmateur
Mar 28 at 15:23




1




1





@ScienceAmateur Neither your question nor your example discuss that intent at all. Your example only shows you wanting to exit your program when you press Ctlr-C.

– Allen Drennan
Mar 28 at 15:29





@ScienceAmateur Neither your question nor your example discuss that intent at all. Your example only shows you wanting to exit your program when you press Ctlr-C.

– Allen Drennan
Mar 28 at 15:29













@ Allen, I made the example as simple as possible for illustrative purposes. I might have made it a bit too simple.

– ScienceAmateur
Mar 28 at 15:39





@ Allen, I made the example as simple as possible for illustrative purposes. I might have made it a bit too simple.

– ScienceAmateur
Mar 28 at 15:39






Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.




















draft saved

draft discarded















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55399769%2fhow-to-eliminate-extraneous-console-output-after-ctrl-c-in-windows%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript