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
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));
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;
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
CreateThread傳遞多個參數的方法(利用結構體的參數指針)