简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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