多线程有两窗体如下:unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,unit2;type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
 A1:TThread;
begin
A1:=tThread.create(False);
end;end.unit Unit2;interfaceuses
  Classes,SysUtils;type
  A1 = class(TThread)
  private
    { Private declarations }
  protected
    procedure Execute; override;
  end;implementationuses unit1;{ 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 A1.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }{ A1 }procedure A1.Execute;
var
 i:integer;
begin
  { Place thread code here }
 for i:=1 to 200000000 do
  form1.edit1.text:=inttostr(i);end;end.点击form1.Button1出错?什么原因

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
     A:A1;
    begin
    A:=A1.create(False);
    end;end.
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
        Memo1: TMemo;
        Label1: TLabel;
        Label2: TLabel;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}uses TestThread;procedure TForm1.Button1Click(Sender: TObject);
    var
        NewThread: TTestThread;
    begin
        NewThread := TTestThread.Create(False);
    end;end.
    unit TestThread;interfaceuses
      Classes;type
      TTestThread = class(TThread)
      private
        Answer: integer;
        { Private declarations }
      protected
        procedure GiveAnswer;
        procedure Execute; override;
      end;implementationuses SysUtils, Unit1;{ 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 TTestThread.UpdateCaption;
        begin
          Form1.Caption := 'Updated in a thread';
        end; }{ TTestThread }procedure TTestThread.GiveAnswer;
    begin
        Form1.Edit1.Text := IntToStr(Answer);
    end;procedure TTestThread.Execute;
    var
        i: integer;
    begin
        FreeOnTerminate := True;
        for i:=1 to 20000 do
        begin
            if Terminated then
                Break;
            inc(Answer, Round(Abs(Sin(Sqrt(i)))));
            Synchronize(GiveAnswer);
        end;
      { Place thread code here }
    end;end.
      

  3.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      a: A1;//A1:TThread;
    begin
      a:=A1.create(false);//A1:=tThread.create(False);
    end;
      

  4.   

    comerliang(天地良心) 的基本正确,核心问题在于,你原先线程里直接调用了vcl控件,而普通vcl控件不是线程安全的,所以必须要用Synchronize去调用
      

  5.   

    你的程序共同两个问题,分别被杰克逊和天地良心道破,第一个绝对不行,第二个实现上不一定会出错,但那样写确实不应该,用Synchronize还是可靠,谁愿意写个有潜在问题的程序啊