您的位置:首页 > 移动开发 > Objective-C

delphi 中使用WaitForMultipleObjects等待线程执行,再执行后续代码

2016-05-23 09:41 856 查看


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.
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  多线程