张志峰的博客

水滴石川,积少成多。

导航

unit1

[delphi] view plain copy
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
btn1: TButton;
mmo1: TMemo;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

procedure ThreadTest;stdcall;
implementation

uses Unit2;

{$R *.dfm}


procedure ThreadTest;stdcall;
var
Handles:TWOHandleArray;
//Handle:THandle;
Test:TTestThread;
i:Integer;
begin
for i := 0 to 10 do
begin
Test := TTestThread.Create(False);
Handles[i] := Test.Handle;
end;
WaitForMultipleObjects( 11, @Handles, True, INFINITE );
Form1.mmo1.Lines.Add( '123' );
end;


procedure TForm1.btn1Click(Sender: TObject);
var
ID:Cardinal;
begin
CreateThread( nil, 0, @ThreadTest, nil, 0, ID );
end;



end.

 

unit2
[delphi] view plain copy
unit Unit2;

interface

uses
Classes;

type
TTestThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;

implementation

uses Unit1;


procedure TTestThread.Execute;
begin
{ Place thread code here }
{ Place thread code here }
//FreeOnTerminate := False;
form1.mmo1.Lines.Add( 'ok' );

end;

end.

程序执行效果:

ok
ok
ok
ok
ok
ok
ok
ok
ok
ok
ok
123
界面上先打印出11个线程输出的“OK”,再输出123.