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

 

http://www.cnblogs.com/azhqiang/p/3957312.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM