Jump to content
uniGUI Discussion Forums
elGringo

Catch exceptions from thread in UniApplication

Recommended Posts

Hi, in vcl doing like this...

TTestThread = class(TThread)
  private
    fEx : Exception;
    procedure QueryError;
  protected
    procedure Execute; override;
  end;
procedure TTestThread .Execute;
begin
  inherited;
 
  try
    // my code
  except
    fEx := ExceptObject as Exception;
    Synchronize( QueryError );
  end;
end;
procedure TTestThread .QueryError;
begin
  Application.OnException( Self, fEx );
end;

How to do the same in UniGUI?

Share this post


Link to post
Share on other sites

tried this but didn't help

procedure THTTPThread.Execute;
var
  r,a,b: double;

begin
  { Place thread code here }
  //http request
  try
    if Assigned(FHTTPRequestFunction) then
      FHTTPRequestFunction();
    b:=0;
    a:=1;
    r := a / 0; // raises exception here but dont switch to exception block...
//    raise Exception.Create('Error Message');
  except
    on E: Exception do
    begin
      Synchronize(
        procedure
        begin
          UniApplication.UniSession.ShowAlert('Произошла ошибка ' + E.Message + #13#10 + 'Попробуйте ещё раз');
        end)
    end;
  end;

end;

Share this post


Link to post
Share on other sites

also tried this but also didn't help...

      Synchronize(
        procedure
        begin
        if Assigned(FSomeObject) then (FSomeObject as TTestFrame).showException(E.Message);
         //  UniApplication.UniSession.ShowAlert('Произошла ошибка ' + E.Message + #13#10 + 'Попробуйте ещё раз');
        end)

Share this post


Link to post
Share on other sites

are you sure that the problem is the exception catching ?

maybe your problem is only for displaying the error to the user.

Could you try to save your exception in a log file and don't try to display it

Share this post


Link to post
Share on other sites

i'm sending request to http and 1/5 response with error - i want to inform user to repeat this request, so yes, i need it to display

Share this post


Link to post
Share on other sites

tried through procedure of object, also failed...

 TExceptionNotify = procedure(AMsg: string) of object;
procedure THTTPThread.Execute;
var
  r, a, b: double;
begin
  { Place thread code here }
  //http request
  try
    if Assigned(HTTPRequestFunction) then
      HTTPRequestFunction();
    b := 0;
    a := 1;
    r := a / 0;
//    raise Exception.Create('Error Message');
  except
    on E: Exception do
    begin
      Synchronize(
        procedure
        begin
          if Assigned(FExceptionNotify) then
            FExceptionNotify('123');
        end)
    end;
  end;

end;

starting to think that unigui deny such messages from threads... to main thread....

Share this post


Link to post
Share on other sites

now it falls to exception block, but ! when calling FExceptionNotify it raises new exception... with AV

and i call with if Assigned(FExceptionNotify) - gently, so it seems to me problem somewhere in unigui

Share this post


Link to post
Share on other sites

so guys, if you have ideas, please share, wanna try working one if somebody knows what's up...

all i want to send request in thread, because requests take 3-10 seconds and sometimes the fall to socket error or bad request - so i want to inform my user what's up, i don't want my user to wait this time

Share this post


Link to post
Share on other sites

i don't say to not use display the problem to user.

 

I think that your problem is not to catch the exception. You could verify this with using a log file and not display for test. i think you would be have lines with exception in log file with this method.

i think that the problem is to synchronize server side with client side for display the error message.

 

 

maybe it is better to use callbacks here rather than threads?

Maybe ?

 

maybe threadTimer exemple could help you ?

Share this post


Link to post
Share on other sites

so you see sessions don't give message from one thread to another (

so i put antifreeze component and handled on beginWork event and onEndWork with moving progressBar, but!

it begin working in GUI right after idHTTP processed the request

 

i fill myself on the moon, seems to be Delphi but not Delphi, pseudo Delphi in some places )))

Share this post


Link to post
Share on other sites

have an idea to use indy http server and use idea of webhooks with back links (when job is done 2-d server will send ajax request to unigui if, of course unigui will allow to get request from other servers...)

Share this post


Link to post
Share on other sites

solved in, when setup another vcl app with idHttpServer  and sent requests there...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×