procedure TMyDialog.createSpecificThread(methodAddr: Pointer;
var Parameter: TFileTransferList);
var
AThreadHandle : Integer;
//Param : PParam;
begin
{New(Param);
Param^.AObject := Parameter;}
AThreadHandle := beginThread(methodAddr, Parameter);
if (AThreadHandle > 0)
then
begin
// warten, bis sich der Thread geschlossen hat
if (WaitForSingleObject(AThreadHandle, INFINITE) <> WAIT_OBJECT_0)
then
begin
ShowMsg('
ERROR(WaitForSingleObject): Cannot close thread!', '
', MB_ICONERROR);
end;
CloseHandle(AThreadHandle);
end;
end;
[...]
function TNonVCLThread.beginThread(methodAddr: Pointer; Parameter: Pointer): Integer;
var
method : TMethod;
ThreadID : Cardinal;
begin
method.Code := methodAddr;
method.Data := self;
// converts method to function pointer
FProcInst := MakeProcInstance(method);
// creates the thread
result := System.BeginThread(
nil, 0, FProcInst, Parameter, 0, ThreadID);
end;
[...]
// Aufruf
procedure TMyDialog.SaveFiles;
var
files: TFileTransferList;
begin
files := TFileTransferList.Create();
[...]
// files.add usw.
createSpecificThread(@TMyDialog.SaveThread, files);
end;
[...]
function TMyDialog.SaveThread(Ptr: Pointer): DWORD;
stdcall;
var
Param : PParam;
files : TFileTransferList;
len : Integer;
begin
result := 0;
{Param := PParam(Ptr);
try
files := TFileTransferList(Param^.AObject);
finally
Dispose(Param);
end;}
files := TFileTransferList(Ptr);
try
BytesCount := 0;
len := files.Count;
// <- AV bzw. ein ungültiger int-Wert
for i := 0
to len-1
do
[...]
end;