Root > How to... > ...add an event handler? > ...register event handler from code?

...register event handler from code?

Previous pageReturn to chapter overviewNext page   

Important Note: NEVER assume thread in which your event handler will run. Your event handler may be called from a background thread even in single-threaded application. For example, a stack overflow exception will force all event handlers to run from a background service thread. Conclusion: always use proper synchronization when accessing global entities from your event handlers.

 

 

Using TEurekaLogEvents component is a simple way to register your event handler. However, it is not enough if you want to react to events when form is not created. You can register event handler manually from your code.

 

You need to include EEvents unit in uses clause. And you need to register your event handler via one of RegisterEventXYZ procedures:

 

uses
  EEvents;
 
procedure MyExceptionNotifyHandler(const ACustom: Pointer; 

  AExceptionInfo: TEurekaExceptionInfo; 

  var AHandle: Boolean; 

  var ACallNextHandler: Boolean);
begin
  // your code here
end;
 
initialization
  RegisterEventExceptionNotify(nil, MyExceptionNotifyHandler);
end.

 

You can see a description of each available event here. You are usually interested in the OnExceptionNotify event. OnExceptionNotify event is an OnException-style event.

 

Each event is represented as method and procedural type - ending in ...Meth and ...Proc correspondingly. For example, OnExceptionNotify event is represented by:

TELEvExceptionNotifyMeth
TELEvExceptionNotifyProc

As you may already guessed: method types are used for TEurekaLogEvents component; procedural types are used for manual registration from code. However, nothing stops you from registering method event handler from code too.

 

Important Notes:

If you are going to change EurekaLog's settings from your event handler - be sure to alter local options (AExceptionInfo.Options), not global options (CurrentEurekaLogOptions). See this article for explanation.
If you enable EurekaLog for background threads - your event handlers will be called in the context of corresponding thread. E.g. if exception happens in a background thread - your event handlers will be called by this thread. Therefore, you need to use some sort of synchronization if you are going to access global data. For example, if you want to access forms (VCL) in your event handlers - then you must marshal these calls to the main thread (for example, by using SendMessage, TThread.Synchronize, or TThread.Queue).
Leaks checking happens at a very special time in your application: everything is already dead (finalized). That is why general event handlers will not run, as people usually write handler's code without such strict restrictions. However, you can register event handler for leaks checking.

 

 

See also:




Send feedback... Build date: 2023-09-11
Last edited: 2023-08-09
PRIVACY STATEMENT
The documentation team uses the feedback submitted to improve the EurekaLog documentation. We do not use your e-mail address for any other purpose. We will remove your e-mail address from our system after the issue you are reporting has been resolved. While we are working to resolve this issue, we may send you an e-mail message to request more information about your feedback. After the issues have been addressed, we may send you an email message to let you know that your feedback has been addressed.


Permanent link to this article: https://www.eurekalog.com/help/eurekalog/how_to_register_event_handler.php