나는 어떻게 처리 Ctrl+C 델피를 콘솔 프로그램?
문제
이 있는 최고의 관행할 수 있는 코드 조각을 보여주는 어떻게 처리할 수 있습 Ctrl+C 델피를 콘솔 프로그램?
내가 찾는 일부 기사는 일부를 제공한 정보에 대해 가능한 문제를 디버거와 함께,예외 처리,하역의 Dll 을 닫의 stdin 및 종료 예를 들어 이 CodeGear 포럼 스레드.
해결책
윈도우(APIMSDN):
BOOL WINAPI SetConsoleCtrlHandler(
PHANDLER_ROUTINE HandlerRoutine, // address of handler function
BOOL Add // handler to add or remove
);
A HandlerRoutine 함수는 콘솔 프로세스를 지정을 처리하 제어 신호를 수신하여 처리합니다.함수 이름은 임의로 지정할 수 있습니다.
BOOL WINAPI HandlerRoutine(
DWORD dwCtrlType // control signal type
);
델파이의 처리기는 일과 같아야 한다:
function console_handler( dwCtrlType: DWORD ): BOOL; stdcall;
begin
// Avoid terminating with Ctrl+C
if ( CTRL_C_EVENT = dwCtrlType ) then
result := TRUE
else
result := FALSE;
end;
다른 팁
내가 쓴 작은 프로그램을 중지하는 방법을 제대로 배경에는 작업입니다.희망은 명확하다.
의 콘텐츠 ThreadConsoleApplication.조선 민주주의 인민 공화 file:
program ThreadConsoleApplication;
{$APPTYPE CONSOLE}
uses
SysUtils,
Windows,
Classes;
type
{ **
* Classe TQueueReaderTestApplication
* }
TThreadConsoleApplication = class(TThread)
public
procedure Execute; override;
constructor Create; virtual;
destructor Destroy; override;
class function getInstance: TThreadConsoleApplication;
end;
function TThreadConsoleApplication_consoleCtrlHandler(dwCtrlType: DWORD): BOOL;
stdcall; forward;
{ **
* Classe TQueueReaderTestApplication
* }
constructor TThreadConsoleApplication.Create;
begin
inherited Create(True { CreateSuspended } );
Windows.setConsoleCtrlHandler(@TThreadConsoleApplication_consoleCtrlHandler,
True { add } );
end;
var
threadConsoleApplicationInstance: TThreadConsoleApplication = nil;
destructor TThreadConsoleApplication.Destroy;
begin
threadConsoleApplicationInstance := nil;
inherited;
end;
procedure TThreadConsoleApplication.Execute;
begin
System.Writeln('[TThreadConsoleApplication.Execute] begin');
try
while not Terminated do
begin
System.Writeln('[TThreadConsoleApplication.Execute] running ...');
Windows.Sleep(1000 { dwMilliseconds } );
end;
finally
System.Writeln('[TThreadConsoleApplication.Execute] end');
end;
end;
class function TThreadConsoleApplication.getInstance: TThreadConsoleApplication;
begin
if nil = threadConsoleApplicationInstance then
begin
threadConsoleApplicationInstance := TThreadConsoleApplication.Create;
end;
Result := threadConsoleApplicationInstance;
end;
function TThreadConsoleApplication_consoleCtrlHandler(dwCtrlType: DWORD): BOOL;
begin
Result := False;
if Windows.CTRL_C_EVENT = dwCtrlType then
begin
TThreadConsoleApplication.getInstance.Terminate;
Result := True;
end;
end;
var
thread: TThread;
begin
System.Writeln('[program] begin');
try
thread := nil;
try
thread := TThreadConsoleApplication.getInstance;
thread.Resume;
System.Writeln('[program] press a CTRL+C to stop running');
thread.WaitFor;
finally
thread.Free;
end;
System.Writeln('[program] end');
except
on E: Exception do
begin
System.Writeln(System.ErrOutput, '[program] end with error');
System.Writeln(System.ErrOutput, E.ClassName, ': ', E.Message);
end;
end;
System.Writeln('[program] press a key to quit');
System.Readln;
end.
제휴하지 않습니다 StackOverflow