请问各位大哥,谁有多线程的例子?

解决方案 »

  1.   

    看看《delphi程序员参考》
    其实很简单。
    到google上搜一搜吧。
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, TFlatButtonUnit, ExtCtrls, TFlatPanelUnit;type
      TForm1 = class(TForm)
        Panel: TPanel;
        BallBtn: TButton;
        procedure BallBtnClick(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementationuses Unit2;{$R *.dfm}procedure TForm1.BallBtnClick(Sender: TObject);
    var
      BallShape : TShape;
    begin
      BallShape := TShape.Create(Self);
      BallShape.Parent := Panel;
      BallShape.Shape := stCircle;
      BallShape.Height := 10;
      BallShape.Width := 10;
      Randomize;
      BallShape.Brush.Color := RGB (Byte(random(255)),
                                    Byte(random(255)),
                                    Byte(random(255)));
      TBall.Create (BallShape,Random(100),Random(100));
    end;end.unit Unit2;interfaceuses
      Classes,ExtCtrls;type
      TBall = class(TThread)
      private
        { Private declarations }
        Ball : TShape;
        SpeedX : Integer;
        SpeedY : Integer;
        procedure MoveBall ;
      protected
        procedure Execute; override;
      public
        constructor Create ( BallShape:TShape ;
                             X,Y : Integer);
      end;implementationuses Unit1;{ Important: Methods and properties of objects in VCL or CLX can only be used
      in a method called using Synchronize, for example,      Synchronize(UpdateCaption);  and UpdateCaption could look like,    procedure TBall.UpdateCaption;
        begin
          Form1.Caption := 'Updated in a thread';
        end; }{ TBall }constructor TBall.Create(BallShape: TShape; X, Y: Integer);
    begin
      Ball := BallShape;
      Ball.Left := X;
      Ball.Top := Y;
      inherited  Create(False );
    end;procedure TBall.Execute;
    begin
      { Place thread code here }
      synchronize (MoveBall);
    end;procedure TBall.MoveBall;
    begin
      if not Terminated then
      begin
        with Ball do
        begin
          randomize;
          SpeedX := Random (10);
          SpeedY := Random (10);
          Left := Left + SpeedX;
          Top := Top + SpeedY;
          if (Left < 0 ) or (Left >( Form1.Panel.Left + Form1.Panel.Width )) then
          SpeedX := -SpeedX;
          if (Top < 0 ) or (Top >( Form1.Panel.Top + Form1.Panel.Height )) then
          SpeedY := -SpeedY;
        end;
      end;
    end;end.一个随便实验的程序,不好,请勿见笑