对这个完全不懂,谁来写小段代码给我学习下呢比如从1加到1万的循环,在循环时窗体可以拖动,我知道可以用app....那方法我只是想看看多线程应该如何写。谢谢

解决方案 »

  1.   

    /////////////////////线程单元unit Unit2;interfaceuses
      SysUtils,Classes;type
      Test = class(TThread)
      private
        { Private declarations }
      protected
        procedure Execute; override;
        procedure UpdateEdit;
      end;implementationuses Unit1;
    var
        i:integer;{ Important: Methods and properties of objects in visual components can only be
      used in a method called using Synchronize, for example,      Synchronize(UpdateCaption);  and UpdateCaption could look like,    procedure Test.UpdateCaption;
        begin
          Form1.Caption := 'Updated in a thread';
        end; }{ Test }
    procedure Test.UpdateEdit;
    begin
        Form1.Edit1.Text:=IntToStr(i);
    end;procedure Test.Execute;
    begin
      { Place thread code here }
        for i:=0 to 10000 do
        begin
            if Terminated then
                break;
            Synchronize(UpdateEdit);
            Sleep(10);
        end;
    end;end.
    ////////////////////////
    主程序单元:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementationuses Unit2;{$R *.dfm}
    var
        thTest:Test;
    procedure TForm1.Button1Click(Sender: TObject);begin
        thTest:=Test.Create(true);
        thTest.Resume;
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
        thTest.Terminate;
        thTest.Free;
    end;end.
      

  2.   

    我的最简单 :-)TT = Class(TThread)
    end;