CreateThread传递多个参数的方法

很多朋友一直都在问CreateThread如何传递多个参数,CreateThread传递参数的方式是指针传递的,所以这里也可以利用指针来做!Demo 关键代码如下:

type
  TfrmTestThread = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  PParData = ^TParData;

  TParData = record
    Count: Integer;
    TestStr: string[100];
  end;

var
  frmTestThread: TfrmTestThread;

implementation

{$R *.dfm}

function TestThread(AParData: PParData): Boolean; stdcall;
var
  I: Integer;
  DC: HDC;
  TestStr: string;
begin
  DC := GetDC(frmTestThread.Handle);
  SetBkColor(DC, GetSysColor(COLOR_BTNHIGHLIGHT));
  for I := 1 to AParData.Count do
  begin
    TestStr := AParData.TestStr + IntToStr(I);
    TextOut(DC, 10, 10, PChar(TestStr), Length(TestStr));
  end;
  ReleaseDC(frmTestThread.Handle, DC);
end;

procedure TfrmTestThread.Button1Click(Sender: TObject);
var
  hThread: THandle;
  ThreadID: DWord;
  vParData: PParData;
begin
  new(vParData);
  vParData.Count := 10000;
  vParData.TestStr := '多线程测试';
  hThread := CreateThread(nil, 0, @TestThread, vParData, 0, ThreadID);
  if hThread = 0 then
    MessageBox(Handle, '创建失败!', nil, MB_OK);
end;

end.

http://www.xuedelphi.cn/article/html2010/2010091922162863.html

你可能感兴趣的:(thread)