我正在处理这种类型的动作队列线程,我想等待某个动作被执行。我想在主线程中创建动作,然后将其传递给队列线程函数(到队列末尾)并等待执行此动作。所以我需要区分我刚才查询的行为已经执行并等待它。将事件从主线程传递给工作线程并等待它是否安全?
我有一个以下(伪)代码,我想知道
- 是否与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