procedure TForm1.Button1Click(Sender: TObject);
var
ThreadID:DWORD;
hThread:Thandle;
begin
hThread:=CreateThread(nil,0,@settext,nil,0,ThreadID);
end;
procedure TForm1.settext;
begin
edit1.Text :='测试';
end;Error:Variable required
初学线程,多多指教

解决方案 »

  1.   

    类的方法不能直接用作CreateThread的第三个参数。
    如果是初学线程编程,建议使用经Delphi封装过的TThread类来实现。
      

  2.   

    jadeluo(秀峰)  应该怎么改下了?
      

  3.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TMyThread = class(TThread)
      private
        FIndex : Integer;
        FMemo  : TMemo;
        procedure AddMemoLine;
      protected
        procedure Execute; override;
      public
        constructor Create(Suspended: Boolean;  iIndex: Integer;  AMemo: TMemo);
      end;  TForm1 = class(TForm)
        Memo1: TMemo;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        iIndex : Integer;
      end;var
      Form1: TForm1;implementation{$R *.dfm}constructor TMyThread.Create(Suspended: Boolean;  iIndex: Integer;  AMemo: TMemo);
    begin
      inherited Create(Suspended);
      FIndex := iIndex;
      FMemo := AMemo;
    end;procedure TMyThread.AddMemoLine;
    begin
      FMemo.Lines.Add(Format('Thread %d', [FIndex]));
    end;procedure TMyThread.Execute;
    begin
      while not Terminated do
      begin
        Synchronize (AddMemoLine);
        Sleep(1000);
      end;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      iIndex := 0;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      with TMyThread.Create(True, iIndex, Memo1) do
      begin
        FreeOnTerminate := True;
        Resume;
      end;
      inc(iIndex);
    end;end.
      

  4.   

    procedure MyThread(p: Pointer); stdcall;
    begin
      //...
    end;var
      h, thid: Cardinal;
    h := CreateThread(nil, 0, @MyThread, nil, 0, thid);
      

  5.   

    用delphi的TThread派生.菜单: File - New, Thread Object