Jump to content


Photo

Catch exceptions from thread in UniApplication


  • Please log in to reply
20 replies to this topic

#1 elGringo

elGringo

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 373 posts

Posted 26 December 2017 - 01:48 PM

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?


  • 0

#2 elGringo

elGringo

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 373 posts

Posted 26 December 2017 - 02:12 PM

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;


  • 0

#3 elGringo

elGringo

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 373 posts

Posted 26 December 2017 - 02:49 PM

is anybody alive?


  • 0

#4 elGringo

elGringo

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 373 posts

Posted 26 December 2017 - 03:07 PM

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)

  • 0

#5 delagoutte

delagoutte

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 304 posts

Posted 26 December 2017 - 03:24 PM

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


  • 0

#6 elGringo

elGringo

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 373 posts

Posted 26 December 2017 - 03:26 PM

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


  • 0

#7 elGringo

elGringo

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 373 posts

Posted 26 December 2017 - 03:28 PM

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....


  • 0

#8 elGringo

elGringo

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 373 posts

Posted 26 December 2017 - 03:29 PM

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


  • 0

#9 elGringo

elGringo

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 373 posts

Posted 26 December 2017 - 03:36 PM

finally TNotifyEvent falls to AV

...
if Assigned(FNotifyEvent) then FNotifyEvent(nil);
...



  • 0

#10 elGringo

elGringo

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 373 posts

Posted 26 December 2017 - 03:37 PM

kind of mystery for me...


  • 0

#11 elGringo

elGringo

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 373 posts

Posted 26 December 2017 - 03:43 PM

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


  • 0

#12 elGringo

elGringo

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 373 posts

Posted 26 December 2017 - 03:45 PM

for the moment i cannot even inform that thread is over ((


  • 0

#13 elGringo

elGringo

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 373 posts

Posted 26 December 2017 - 03:53 PM

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


  • 0

#14 delagoutte

delagoutte

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 304 posts

Posted 26 December 2017 - 04:38 PM

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 ?


  • 0

#15 elGringo

elGringo

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 373 posts

Posted 26 December 2017 - 05:29 PM

limitations of framework...

Attached Files

  • Attached File  27.JPG   40.28KB   5 downloads

  • 0

#16 elGringo

elGringo

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 373 posts

Posted 26 December 2017 - 05:32 PM

threadTimer

 

but how?


  • 0

#17 elGringo

elGringo

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 373 posts

Posted 26 December 2017 - 05:35 PM

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 )))


  • 0

#18 elGringo

elGringo

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 373 posts

Posted 26 December 2017 - 05:54 PM

tried so send httpRequest from thread - catched AV... seems to be its forbidden


  • 0

#19 elGringo

elGringo

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 373 posts

Posted 26 December 2017 - 05:56 PM

seems to me for background job its better use neighbour server...


  • 0

#20 elGringo

elGringo

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 373 posts

Posted 26 December 2017 - 06:00 PM

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...)


  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users