toufik 2 Posted September 14, 2020 Hi all Delphier , good morning 😀😀 i have rest firemonkey app ,server , client im trying to check if the server is up or down from the client side using the code below , the code work fine , but when i run it its freeze the app for a while is there any proper way to do this ,thank you ,🤓☺️ try RequestLogin.Execute; if FrmLogin.RequestLogin.Response.Status.Success then begin showmessage ('server up') do something ..... end else except on e : exception do begin showmessage (' server down.... go home ') end end; Share this post Link to post
Remy Lebeau 1398 Posted September 14, 2020 8 hours ago, toufik said: i have rest firemonkey app ,server , client im trying to check if the server is up or down from the client side The best way to handle that is to perform an actual REST request, and handle any error it may produce. 8 hours ago, toufik said: the code work fine , but when i run it its freeze the app for a while Move the code to a worker thread so that it doesn't block the UI thread. 1 Share this post Link to post
toufik 2 Posted September 14, 2020 @Remy Lebeau i will try it ,,,,,thank you and ,,, if there any example to how to do this , it will be helpful thank you Share this post Link to post
Remy Lebeau 1398 Posted September 14, 2020 (edited) 2 hours ago, toufik said: if there any example to how to do this , it will be helpful The simplest option is to use either TThread.CreateAnonymousThread(), eg: uses ..., System.Classes; procedure TFrmLogin.StartLogin; begin TThread.CreateAnonymousThread( procedure var Success: Boolean; begin try RequestLogin.Execute; Success := RequestLogin.Response.Status.Success; except Success := False; end; TThread.Queue(nil, procedure begin AfterLogin(Success); end ); end ).Start; end; procedure TFrmLogin.AfterLogin(Success: Boolean); begin if Success then begin ShowMessage ('server up'); do something ... end else begin ShowMessage ('server down'); do something else ... end; end; Or similarly, TTask.Run(): uses ..., System.Threading; procedure TFrmLogin.StartLogin; begin TTask.Run( procedure var Success: Boolean; begin try RequestLogin.Execute; Success := RequestLogin.Response.Status.Success; except Success := False; end; TThread.Queue(nil, procedure begin AfterLogin(Success); end ); end ); end; procedure TFrmLogin.AfterLogin(Success: Boolean); begin if Success then begin ShowMessage ('server up'); do something ... end else begin ShowMessage ('server down'); do something else ... end; end; Edited September 14, 2020 by Remy Lebeau 1 Share this post Link to post
toufik 2 Posted September 18, 2020 On 9/15/2020 at 1:05 AM, Remy Lebeau said: The simplest option is to use either TThread.CreateAnonymousThread(), eg: uses ..., System.Classes; procedure TFrmLogin.StartLogin; begin TThread.CreateAnonymousThread( procedure var Success: Boolean; begin try RequestLogin.Execute; Success := RequestLogin.Response.Status.Success; except Success := False; end; TThread.Queue(nil, procedure begin AfterLogin(Success); end ); end ).Start; end; procedure TFrmLogin.AfterLogin(Success: Boolean); begin if Success then begin ShowMessage ('server up'); do something ... end else begin ShowMessage ('server down'); do something else ... end; end; Or similarly, TTask.Run(): uses ..., System.Threading; procedure TFrmLogin.StartLogin; begin TTask.Run( procedure var Success: Boolean; begin try RequestLogin.Execute; Success := RequestLogin.Response.Status.Success; except Success := False; end; TThread.Queue(nil, procedure begin AfterLogin(Success); end ); end ); end; procedure TFrmLogin.AfterLogin(Success: Boolean); begin if Success then begin ShowMessage ('server up'); do something ... end else begin ShowMessage ('server down'); do something else ... end; end; I can't thank you enough ,thank s a lot Share this post Link to post
Uwe Raabe 2057 Posted September 18, 2020 TRestRequest has also a method ExecuteAsync with handlers for successful completion and error handling (in addition to the event handlers OnAfterExecute and OnHTTPProtocolError). Depending on your specific needs it may boil down to just a simple call like this: RequestLogin.ExecuteAsync( procedure begin ShowMessage('server up'); end, True, True, procedure begin ShowMessage('server down'); end); 2 Share this post Link to post
toufik 2 Posted September 21, 2020 @Uwe Raabe Thank you for reply ^ i will try that too , and of curse the first solution work just fine too . Share this post Link to post