我正在處理這種類型的動作隊列線程,我想等待某個動作被執行。我想在主線程中創建動作,然後將其傳遞給隊列線程函數(到隊列末尾)並等待執行此動作。所以我需要區分我剛纔查詢的行爲已經執行並等待它。將事件從主線程傳遞給工作線程並等待它是否安全?
我有一個以下(僞)代碼,我想知道
- 是否與Windows事件對象的線程安全的工作?
- 如果是的話,這個概念會有效嗎?
type
TMyThread = class(TThread);
private
FEvent: THandle;
protected
procedure Execute; override;
public
procedure DoSomething(const AEvent: THandle);
end;
procedure TMyThread.Execute;
begin
// is it working with events thread safe ?
SetEvent(FEvent);
// the thread will continue, so I can't use WaitFor
// but it won't set this specific FEvent handle again
// I'm working on such kind of an action queue, so once the action with ID,
// here represented by the FEvent will be processed, it's removed from
// the action queue
end;
procedure TMyThread.DoSomething(const AEvent: THandle);
begin
FEvent := AEvent;
end;
// here's roughly what I want to do
procedure TForm1.Button1Click(Sender: TObject);
var
OnceUsedEvent: THandle;
begin
// the thread is already running and it's instantiated in MyThread
// here I'm creating the event for the single request I need to be performed
// by the worker thread
OnceUsedEvent := CreateEvent(nil, True, False, nil);
try
// here I'm passing the event handle to the worker thread (like a kind of
// a request ID)
MyThread.DoSomething(OnceUsedEvent);
// and here I want to wait for 10 seconds (and also interrupt this waiting
// when the user closes the application if possible ?) for the thread if
// performs my request
WaitForSingleObject(OnceUsedEvent, 10000);
finally
// close the event handle
CloseHandle(OnceUsedEvent);
end;
// and continue with something else
end;
謝謝!
在按鈕事件處理程序中等待事件將阻止主線程,所以這不是你想要的!也許你可以使用事件代替(線程完成時調用) – jpfollenius 2012-03-02 15:43:57
這樣的等待不會做你想做的事。它不能被打斷。你爲什麼要阻止10秒?這很奇怪。 – 2012-03-02 15:50:21
理想無限;這是重命名行動。我需要進入VirtualTreeView節點的編輯模式並保持編輯器處於活動狀態,直到從線程獲得重命名操作的結果(我有一個額外的事件處理程序,如果重命名成功並且我退出時需要傳遞結果這個事件處理程序編輯器被隱藏)。 – 2012-03-02 16:02:29