I had a funny idea this weekend, I was looking for a way to multithread a part of my code without having to break my code.
Using an anonymous method the thread is able to access the local variables of the method that executes the thread.
The trick is very simple and allows great flexibility in the code.
type
TRefProc = reference to procedure;
function Thread(const proc: TRefProc): THandle;
function run(const proc: TRefProc): Integer; stdcall;
begin
proc;
Result := 0;
end;
begin
CreateThread(nil, 0, @run, PPointer(@proc)^, 0, Result);
end;
procedure DoIt;
var
done: THandle;
begin
done := CreateEvent(nil, True, False, nil);
try
Thread(procedure
begin
sleep(2000);
SetEvent(done);
end);
WaitForSingleObject(done, INFINITE);
finally
CloseHandle(done);
end;
end;
I have a good resolution for this new year I will socialize me.
I am 34 years old and live in Metz in France.
I am self taught for nearly 15 years after abandoning my studies in accounting, and after failing the entrance examination of an art school, the world will never know what artist I could be.
I console myself by saying that what I do is also art.
If you like my work, you can follow me on facebook, twitter and buzz of course. If you can not live without me, there is a button “donate”