Search code examples
delphidelphi-10.2-tokyo

Delphi tethering ResourceReceived fails to update TLabel


Delphi 10.2.3 FMX app tethering

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?

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.


Solution

  • Commonly Delphi events are triggered within main thread (UI controls) or are synchronized with main thread (TThread.OnTernimate - event). 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.

    TTetheringProfile class (ancestor of TTetheringAppProfile) has SynchronizeEvents property (by default set to True) that controls on which thread are events called. If True all event handlers will run in context of main thread.

    Symptoms you are having are consistent with accessing UI from the secondary thread. Check the value of SynchronizeEvents property or synchronize UI access with main thread within your event handler.