如何判断多个线程已经结束? ( 积分: 200 )

  • 主题发起人 主题发起人 yitang
  • 开始时间 开始时间
Y

yitang

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,bytearray, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
Function getthreadcount():word;
private
{ Private declarations }
procedure threaddestroy(Sender:Tobject);
public
{ Public declarations }
end;

var
Form1: TForm1;
ThreadCount:word;

implementation
uses unit2;
{$R *.dfm}
procedure Tform1.threaddestroy(Sender:Tobject);
begin
inc(Threadcount,-1);
end;

function TForm1.getthreadcount():word;
begin
result:=threadcount;
end ;
procedure TForm1.Button1Click(Sender: TObject);
var
n:word;
begin
ThreadCount:=5;//这里简化了,只是想说明最终会创建5个线程
for n:=1 to 5do
begin
with mythread.create(n)do
//线程已经inherited create(false),FreeonTerminate:=true
onterminate:=threaddestroy;
end;
//到这里,我想执行完刚刚创建的5个线程之后再继续下面的的代码,请问如何实现?
//code
...
...
...
end;
end.
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,bytearray, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
Function getthreadcount():word;
private
{ Private declarations }
procedure threaddestroy(Sender:Tobject);
public
{ Public declarations }
end;

var
Form1: TForm1;
ThreadCount:word;

implementation
uses unit2;
{$R *.dfm}
procedure Tform1.threaddestroy(Sender:Tobject);
begin
inc(Threadcount,-1);
end;

function TForm1.getthreadcount():word;
begin
result:=threadcount;
end ;
procedure TForm1.Button1Click(Sender: TObject);
var
n:word;
begin
ThreadCount:=5;//这里简化了,只是想说明最终会创建5个线程
for n:=1 to 5do
begin
with mythread.create(n)do
//线程已经inherited create(false),FreeonTerminate:=true
onterminate:=threaddestroy;
end;
//到这里,我想执行完刚刚创建的5个线程之后再继续下面的的代码,请问如何实现?
//code
...
...
...
end;
end.
 
利用while循环检测[:D]
while not overdo
begin
..
..//里面插点响应代码,可以随时终止这个while
end;
... go on
 
var
H: array[1..5] of THandle;
WaitResult: Cardinal;
begin
for n:=1 to 5do
begin
with mythread.create(n)do
//线程已经inherited create(false),FreeonTerminate:=true
begin
onterminate:=threaddestroy;
H[n] := Handle;
end;
end;

repeat
WaitResult := MsgWaitForMultipleObjects(5, H, True, 1000, QS_SENDMESSAGE or QS_POSTMESSAGE);
case WaitResult of
WAIT_OBJECT_0..WAIT_OBJECT_0+4: Break;
WAIT_OBJECT_0+5: Application.ProcessMessages;
end;
until False;
end;
 
谢谢啦!
 
后退
顶部