[英]Delphi tethering ResourceReceived fails to update TLabel
Delphi 10.2.3 FMX應用共享
以下代碼中的標簽有時會更新,但通常不會更新。 在網絡共享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;
通過將值存儲在AResource.Value.AsString中,然后啟用一個稍后設置標簽文本值的計時器,可以解決該問題。
通常,Delphi事件是在主線程(UI控件)中觸發的,或與主線程同步的( TThread.OnTernimate
事件)。 但是,並非總是如此。
綁定從后台線程進行操作,其事件也從后台線程調用。 另一方面,所有UI訪問必須與主UI線程同步。
TTetheringProfile
類(祖先TTetheringAppProfile
)具有SynchronizeEvents
屬性(默認設置為True
,控制哪個線程被稱為事件)。 如果為True
所有事件處理程序都將在主線程的上下文中運行。
您遇到的症狀與從輔助線程訪問UI一致。 檢查SynchronizeEvents
屬性的值,或將UI訪問與事件處理程序中的主線程同步。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.