在Delphi控制台应用程序中如何处理Ctrl+C?

9

是否有最佳实践和代码片段可用,展示如何在Delphi控制台应用程序中处理Ctrl+C?

我找到了一些文章,其中提供了一些关于调试器可能出现的问题、异常处理、DLL卸载、stdin关闭和终止的信息例如CodeGear论坛线程

2个回答

17

来自 Windows API (MSDN):

BOOL WINAPI SetConsoleCtrlHandler(
    PHANDLER_ROUTINE HandlerRoutine,    // address of handler function  
    BOOL Add    // handler to add or remove 
   );   

HandlerRoutine函数是控制台进程指定来处理该进程接收到的控制信号的函数。该函数可以任意命名。

BOOL WINAPI HandlerRoutine(
    DWORD dwCtrlType    //  control signal type
   );   
在Delphi中,处理程序应该像这样:
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;

2
哦,我懂了。如果你像我的例子一样捕获Ctrl+C,你可以设置一种“标志”,并在任何时候以正常方式终止。 - Nick Dandoulakis
我刚想起来我曾经在我的Turbo Pascal程序中加入了"break := false;"以禁用Ctrl-Break。啊,怀旧…… - Nick Dandoulakis
5
需要翻译的内容:On very important thing to remember is that HandlerRoutine runs in a SEPARATE THREAD. If you don't start any other threads yourself, set the IsMultithread global variable to True so that any memory operations you perform will be safe.重要提醒:HandlerRoutine 运行在一个独立的线程中。如果您没有启动其他线程,请将 IsMultithread 全局变量设置为 True,以便执行任何内存操作时都能保证安全性。 - Rob Kennedy
3
同样重要的是调用约定。回调函数必须使用 stdcall 调用约定,否则你的程序会神秘地崩溃。 - Rob Kennedy
安装此处理程序:Windows.setConsoleCtrlHandler(@console_handler, True {添加}); - Stéphane B.
显示剩余2条评论

4
我写了一个小程序,展示如何正确地停止后台任务。希望这样更清晰易懂。
ThreadConsoleApplication.dpr文件的内容:
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.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接