Delphi 7: How to implement multi-threading?

前端 未结 2 2161
清酒与你
清酒与你 2021-02-11 03:35

I have a TButton in the main TForm. When user click the button, it will execute the below process:

begin
  Process_done := FALSE;

  Process_Result.Clear;

  cmd         


        
2条回答
  •  鱼传尺愫
    2021-02-11 04:00

    Try something like this:

    type
      TRunProcessThread = class(TThread)
      protected
        cmdProcess: Whatever;
        procedure Execute; override;
      public
        constructor Create(const ACmdLine: String);
        destructor Destroy; override;
      end;
    
    constructor TRunProcessThread.Create(const ACmdLine: String);
    begin
      inherited Create(True);
      FreeOnTerminate := True;
      cmdProcess := Whatever.Create;
      cmdProcess.CommandLine := ACmdLine;
    end;
    
    destructor TRunProcessThread.Destroy;
    begin
      cmdProcess.Free;
      inherited;
    end;
    
    procedure TRunProcessThread.Execute;
    begin
      cmdProcess.Run;
      ...
    end;
    

    .

    procedure TForm1.Button1Click(Sender: TObject);
    var
      Thread: TRunProcessThread;
    begin
      Thread := TRunProcessThread.Create(AnsiQuotedStr(AppPath + 'getdata.exe', #34));
      Thread.OnTerminate := ProcessDone;
      Thread.Resume;
    end;
    
    procedure TForm1.ProcessDone(Sender: TObject);
    begin
      // access TRunProcessThread(Sender) to get result information as needed ...
    end;
    

提交回复
热议问题