简体   繁体   English

我可以在工作线程中执行TDataSet.DisableControls而不用Synchronize()包装它吗?

[英]Can I execute TDataSet.DisableControls in worker thread without wrapping it with Synchronize()?

First of all, I am not sure that it is a good design to allow worker thread to disable controls. 首先,我不确定允许工作线程禁用控件是一个好的设计。 However, I am curious can I do it safely without synchronization with GUI? 但是,我很好奇,如果不与GUI同步,我可以安全地做到吗?

The code in TDataSet looks like this: TDataSet中的代码如下所示:

procedure TDataSet.DisableControls;
begin
  if FDisableCount = 0 then
  begin
    FDisableState := FState;
    FEnableEvent := deDataSetChange;
  end;
  Inc(FDisableCount);
end;

So it looks safe to do. 所以看起来很安全。 The situation would be different in case of EnableControls. 在EnableControls的情况下,情况会有所不同。 But DisableControls seems to only increase lock counter and assigning event which is fired up during EnableControls. 但是,DisableControls似乎只会增加锁定计数器并分配在EnableControls期间触发的事件。

What do you think? 你怎么看?

It looks safe to do so, but things may go wrong because these flags are used in code that may be in the middle of being executed at the moment you call this method from your thread. 这样做看起来很安全,但事情可能会出错,因为这些标志用于代码中,这些代码可能在您从线程调用此方法时正在执行。

I would Synchronise the call to DisableControls, because you want your thread to start using this dataset only if no controls are using it. 我会将调用同步到DisableControls,因为只有在没有控件使用它时,您希望线程开始使用此数据集。 The call to EnableControls can be synchronised too, or you can post a message to the form using PostMessage. 对EnableControls的调用也可以同步,或者您可以使用PostMessage将消息发布到表单。 That way, the thread doesn't have to wait for the main thread. 这样,线程就不必等待主线程了。

But my gut feelings tells me that is may be better to not use the same dataset for the GUI and the thread at all. 但我的直觉告诉我,对于GUI和线程根本不使用相同的数据集可能会更好。

Without having looked up the actual code: It might be safe, as long as you can be sure that the main thread currently does not access FDisableCount, FDisableState and FEnableEvent. 没有查找实际代码:它可能是安全的,只要你可以确定主线程当前不访问FDisableCount,FDisableState和FEnableEvent。 There is the possibility of a race condition here. 这里有可能出现竞争状况。

I would still recommend that you call DisableControls from within the main thread. 我仍然建议您从主线程中调用DisableControls。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM