I am working on such an action queue thread, and I would like to wait for a specific action to complete. I would like to create an action in the main thread, and then pass it to the function of the queue thread (until the end of the queue) and wait for this action to be completed. Therefore, I need to distinguish the action that I just requested and is waiting for it.
I have the following (pseudo) code and I would like to know
- Does it work with a stream of Windows event objects?
- if so, will this concept be effective?
type
TMyThread = class(TThread);
private
FEvent: THandle;
protected
procedure Execute; override;
public
procedure DoSomething(const AEvent: THandle);
end;
procedure TMyThread.Execute;
begin
SetEvent(FEvent);
end;
procedure TMyThread.DoSomething(const AEvent: THandle);
begin
FEvent := AEvent;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
OnceUsedEvent: THandle;
begin
OnceUsedEvent := CreateEvent(nil, True, False, nil);
try
MyThread.DoSomething(OnceUsedEvent);
WaitForSingleObject(OnceUsedEvent, 10000);
finally
CloseHandle(OnceUsedEvent);
end;
end;
Thank!
source
share