Check in xConnection is connected

Hello,

I try to check if my connection is connected. I have a Create-procedure where data from the database needed:
procedure TForm_LogIn.WebFormCreate(Sender: TObject);
begin
   if Data_Module.xConnection_connect then begin
      Data_Module.Get_Sprache;
      Label_LoginTitel.DataSource:=Data_Module.DSC_Sprache;
      Label_LoginTitel.DataField:='login_Titel';
      Label_LogInUser.DataSource:=Data_Module.DSC_Sprache;
      Label_LogInUser.DataField:='login_lable_username';
      Label_LogInPasswort.DataSource:=Data_Module.DSC_Sprache;
      Label_LogInPasswort.DataField:='login_label_passwort';
   end;
end;

The function Data_Module.xConnection_connect felivers a boolean:
function TData_Module.xConnection_connect: boolean;
var
   ABlick: TDateTime;
   TimeOut: boolean;
   procedure OnConnect;
   begin
      Result:= true;
   end;
   procedure OnError(Error: TXDataWebConnectionError);
   begin
      Result:= false;
      ShowMessage('XData server connection failed with error: ' +
      Error.ErrorMessage);
   end;
begin
   Result:= false;
   if xConnection.Connected then begin
      Result:= true;
   end
   else begin
      xConnection.Open(@OnConnect, @OnError);
      ABlick:= now();
      TimeOut:= false;
      while (TimeOut=false) and (Result=false) do begin
         if (now()-ABlick > 0.00005) then
            TimeOut:= true;
      end;
   end;
end;

But that way of working does not work. It seems, that the Create-Procedure does not wait for the result of the Data_Module.xConnection_connect function.

In TMS WebCore how can I wait a few secounds for the result?

Thanks Patrick

Is there a possibility / alternative to Application.ProcessMessages ?

That's not how you should do in web applications. Everything should be asynchronous. The correct way is to use the OnConnect event/callback, it will be called when the connection is made. From other parts of the application, you should also use that approach. So for example, this is more or less how you should do from the main form, and modify the xConnection_connect accordingly:




procedure TForm_LogIn.WebFormCreate(Sender: TObject);
begin

   Data_Module.xConnection_connect(procedure
     begin
        Data_Module.Get_Sprache;
        Label_LoginTitel.DataSource:=Data_Module.DSC_Sprache;
        Label_LoginTitel.DataField:='login_Titel';
        Label_LogInUser.DataSource:=Data_Module.DSC_Sprache;
        Label_LogInUser.DataField:='login_lable_username';
        Label_LogInPasswort.DataSource:=Data_Module.DSC_Sprache;
        Label_LogInPasswort.DataField:='login_label_passwort';
     end
  );
end;


Modify your function TData_Module.xConnection_connect to receive a callback and then call the AOnConnect at proper place:


type
  TConnectCallback = reference to procedure;


procedure TData_Module.xConnection_connect(AOnConnect: TConnectCallback);
var


   ABlick: TDateTime;
   TimeOut: boolean;
   procedure OnConnect;
   begin
      if Assigned(AOnConnect) then AOnConnect();
   end;
   procedure OnError(Error: TXDataWebConnectionError);
   begin
      ShowMessage('XData server connection failed with error: ' +
      Error.ErrorMessage);
   end;
begin
   if xConnection.Connected then begin
      if Assigned(AOnConnect) then AOnConnect();
   end
   else begin
      xConnection.Open(@OnConnect, @OnError);
   end;
end;

Wagner R. Landgraf2019-11-27 20:02:10