RTC Forums

Subscription => Support => Topic started by: mastinfo on February 05, 2019, 11:02:20 AM



Title: Updating user interface in a TrtcFunction.execute event
Post by: mastinfo on February 05, 2019, 11:02:20 AM
I have written a backup utlity where RTC is used to comunicate between a service and a desktop app.
I want that the service send back a feedback to the desktop app to show the current status of the backup.
In the desktop app i have defined a remote function that is executed in the service app.
In this function i want to visualize the feedback using a Tlistbox. Unfortunately then the function is executed the desktop app freeze.

Any suggestion ?

thanks

procedure TrtcClient_dm.feedback_fnExecute(Sender: TRtcConnection;
  Param: TRtcFunctionInfo; Result: TRtcValue);
var
  txt:string;
begin
  txt:=param.asValue['txt'];

  if assigned(myform) then
  begin
    aform.alistbox.Items.Add(txt);
  end;
end;


Title: Re: Updating user interface in a TrtcFunction.execute event
Post by: D.Tkalcec (RTC) on February 05, 2019, 11:14:52 AM
If your communication component is Multi-Threaded, then all OnExecute events on all TRtcFunction components linked to that component will be called from background threads. If you want to access the GUI from within your event, you need to synchronize your event with the Main Thread. IOW, if you want to access a listbox on your form from within your "feedback_fnExecute" event, you should add a call to the "Sync" method at the top of your event, like this ...

procedure TrtcClient_dm.feedback_fnExecute(Sender: TRtcConnection;
  Param: TRtcFunctionInfo; Result: TRtcValue);
var
  txt:string;
begin

  if Sender.Sync(feedback_fnExecute,Param,Result) then Exit;

  txt:=param.asValue['txt'];

  if assigned(myform) then
  begin
    aform.alistbox.Items.Add(txt);
  end;
end;

... the "Sync" method checks if the code is being executed from a background RTC thread. If it is, then the event (which is provided as the 1st parameter) will be called with the rest of the parameters in the Main Thread, after which the Sync method will be returning TRUE as the result. If the code is being executed from the Main Thread, then the Sync method does nothing and returns FALSE. As a result, using this line of code at the beginning of your event ensures that the rest of your event code will be executed from the Main Thread.

If this does NOT solve your problem, then I need more details about the exact communication flow. In other words, I need the complete picture and not just the code of the event that results in freezing your desktop app. Since you've mentioned a Server calling a remote function on the Client, I need to know exactly how the Client is making the original call to the Server and how the Server is triggering this "callback".


Title: Re: Updating user interface in a TrtcFunction.execute event
Post by: mastinfo on February 05, 2019, 12:44:36 PM
Hello, thanks.

The problem was that the MultiThreaded property of the TTRtcHttpClient component (in the desktop app) was false.

Roberto