Delphi 7:如何实现多线程? [英] Delphi 7: How to implement multi-threading?

查看:1129
本文介绍了Delphi 7:如何实现多线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主TForm中有一个TButton。当用户点击按钮时,它将执行以下过程:

  begin 
Process_done:= FALSE;

Process_Result.Clear;

cmdProcess.CommandLine:=#34 + AppPath +'getdata.exe';

cmdProcess.Run;

重复
应用程序.ProcessMessages;
直到Process_done;
end;

正如你可以看到的,该进程调用外部可执行文件,并且该进程可能需要一些阻塞主应用程序的次数。



这只是一个进程,我需要另一个进程。 >

所以,我正在考虑实现多线程,我可以在一个单独的线程中运行上面的过程,另一个进程也是,主线程可以做一些WHILE检查这两个进程都完成了。



任何人都可以给我一些使用Delphi 7的例子?



OR指向一篇文章,这样简单的实现?



谢谢。

解决方案

尝试这样的事情:

 键入
TRunProcessThread = class(TThread)
protected
cmdProcess:Whatever;
程序执行;覆盖
public
构造函数Create(const ACmdLine:String);
析构函数覆盖
结束

构造函数TRunProcessThread.Create(const ACmdLine:String);
begin
继承Create(True);
FreeOnTerminate:= True;
cmdProcess:= Whatever.Create;
cmdProcess.CommandLine:= ACmdLine;
结束

析构函数TRunProcessThread.Destroy;
begin
cmdProcess.Free;
继承;
结束

程序TRunProcessThread.Execute;
begin
cmdProcess.Run;
...
end;

  procedure TForm1.Button1Click(Sender:TObject); 
var
线程:TRunProcessThread;
begin
线程:= TRunProcessThread.Create(AnsiQuotedStr(AppPath +'getdata.exe',#34));
Thread.OnTerminate:= ProcessDone;
Thread.Resume;
结束

procedure TForm1.ProcessDone(Sender:TObject);
begin
//访问TRunProcessThread(Sender)以获取需要的结果信息...
end;


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;

  cmdProcess.CommandLine := #34+AppPath+'getdata.exe"';

  cmdProcess.Run;

  Repeat
    Application.ProcessMessages;
  Until Process_done;
end;

As you can see above, the process calls external executable, and the process can take some times which blocking the main application.

This is only one process, and I need another one.

So, I am thinking to implement multi-threading, where I can run the above process in a separate thread. The other process as well. And the main thread can do something WHILE checking when both processes done.

Can anyone give me some examples how to do this using Delphi 7?

OR point me to an article, simple implementation like this?

Thanks.

解决方案

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;

这篇关于Delphi 7:如何实现多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆