1

I am trying to use AsyncCalls which suppose to work for Delphi 5 also.

Everything compiles and runs fine, if I comment the lines that call/use ApplicationHandleException classes variable which Delphi 5 does not have (I'm not sure when it was introduced either).

procedure TThreadPool.MainThreadWndProc(var Msg: TMessage);
begin
  try
    ...
  except
    if Assigned(ApplicationHandleException) then
      ApplicationHandleException(Self); 
  end;
end;

I'm almost sure that in Delphi 5 the above should be:

  try
    ...
  except
      Application.HandleException(Self); 
  end;

But not sure what to do about this code:

destructor TInternalAsyncCall.Destroy;
begin
  ...
  // TAsyncCall.Destroy either already called Sync() or we are a "forgotten" async call
  // and we need to handle the exception ourself by trying to throw it in the main thread
  // or just ignoring it.
  if FFatalException <> nil then
  begin
    if Assigned(ApplicationHandleException) and // <---
       (ThreadPool.FMainThreadVclHandle <> 0) and IsWindow(ThreadPool.FMainThreadVclHandle) then
      PostMessage(ThreadPool.FMainThreadVclHandle, WM_RAISEEXCEPTION, WPARAM(FFatalErrorAddr), LPARAM(FFatalException))
    else
      FFatalException.Free;
  end;
  inherited Destroy;
end;

What could be the correct "translation" in Delphi 5? Should I simply ignore this variable since it does not exists? please advice.

1
  • 2
    "I'm not sure when it was introduced" - the ApplicationHandleException callback was introduced in Delphi 6, as part of the re-organization of the VCL to support CLX in Kylix. Commented Apr 3, 2017 at 15:47

1 Answer 1

2

In TApplication.Create there is this code which assigns to the ApplicationHandleException variable.

if not Assigned(System.Classes.ApplicationHandleException) then
  System.Classes.ApplicationHandleException := HandleException;

Now HandleException is really Self.HandleException, when you allow for the implicit Self target.

So yes, you can replace:

if Assigned(ApplicationHandleException) then
  ApplicationHandleException(Self); 

with

if Assigned(Application) then
  Application.HandleException(Self); 

In general then, for Delphi 5, you would replace

Assigned(ApplicationHandleException)

with

Assigned(Application)

Almost always it will be the case that Assigned(Application) evaluates to True, but you might be working in a non VCL setting, or perhaps the code executes before the global Application object is created, or after it is destroyed.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much. your knowledge is outstanding!
BTW, do you know where else the ApplicationHandleException could be assigned by the framework? I mean, is it assigned differently for a service application for example? (just guessing...)
FMX application, TIdHTTPWebBrokerBridgeRequestHandler.Create, TSockWebRequestHandler.Create, TApacheApplication.Create, TCGIFactory.Create, TWebApplication.Create, TISAPIApplication.Create, TAppletApplication.Run. But not service applications it seems.
So it seems that Application in some cases will not work as designed. never mind I'll think about it when I get there :-P
You probably need to look at how exceptions are handled in those other application types in D5. Quite a few won't exist I suspect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.