简体   繁体   English

Delphi绑定资源Received无法更新TLabel

[英]Delphi tethering ResourceReceived fails to update TLabel

Delphi 10.2.3 FMX app tethering Delphi 10.2.3 FMX应用共享

Sometimes the label in the following code gets updated but often it does not. 以下代码中的标签有时会更新,但通常不会更新。 Is it not safe to update a visual component during the tethering RescourceReceived procedure? 在网络共享RescourceReceived过程中更新视觉组件是否不安全?

procedure TMainForm.MyTetheringAppProfileResourceReceived(
  const Sender: TObject; const AResource: TRemoteResource);

begin
  if AResource.Hint = 'InfoPrincipleVariation'
    then
      begin

        MyInformationLabel.Text := AResource.Value.AsString;  // Fails to update
        Exit;
      end;
end;

I got around the problem by storing the value in AResource.Value.AsString and then enabling a timer that later set the value of the label's text. 通过将值存储在AResource.Value.AsString中,然后启用一个稍后设置标签文本值的计时器,可以解决该问题。

Commonly Delphi events are triggered within main thread (UI controls) or are synchronized with main thread ( TThread.OnTernimate - event). 通常,Delphi事件是在主线程(UI控件)中触发的,或与主线程同步的( TThread.OnTernimate事件)。 However that is not always the case. 但是,并非总是如此。

Tethering operates from background thread and its event are also called from background thread. 绑定从后台线程进行操作,其事件也从后台线程调用。 On the other hand, all UI access must be synchronized with main UI thread. 另一方面,所有UI访问必须与主UI线程同步。

TTetheringProfile class (ancestor of TTetheringAppProfile ) has SynchronizeEvents property (by default set to True ) that controls on which thread are events called. TTetheringProfile类(祖先TTetheringAppProfile )具有SynchronizeEvents属性(默认设置为True ,控制哪个线程被称为事件)。 If True all event handlers will run in context of main thread. 如果为True所有事件处理程序都将在主线程的上下文中运行。

Symptoms you are having are consistent with accessing UI from the secondary thread. 您遇到的症状与从辅助线程访问UI一致。 Check the value of SynchronizeEvents property or synchronize UI access with main thread within your event handler. 检查SynchronizeEvents属性的值,或将UI访问与事件处理程序中的主线程同步。

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

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