代码之家  ›  专栏  ›  技术社区  ›  Mike at Bookup

Delphi tethering ResourceReceived无法更新TLabel

  •  1
  • Mike at Bookup  · 技术社区  · 6 年前

    Delphi 10.2.3 FMX应用程序栓系

    有时下面代码中的标签会得到更新,但通常不会。在栓系重建过程中更新可视组件是否不安全?

    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;
    

    我通过将值存储在AResource.value.AsString中,然后启用一个计时器来设置标签文本的值,从而绕过了这个问题。

    1 回复  |  直到 6 年前
        1
  •  5
  •   Dalija Prasnikar    6 年前

    TThread.OnTernimate -事件)。然而,情况并非总是如此。

    Tethering从后台线程操作,其事件也从后台线程调用。另一方面,所有UI访问必须与主UI线程同步。

    TTetheringProfile 类(类的祖先) TTetheringAppProfile )有 SynchronizeEvents True 是的 所有事件处理程序都将在主线程的上下文中运行。

    您的症状与从辅助线程访问UI一致。检查 属性或将UI访问与事件处理程序中的主线程同步。