Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 666095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:46:51+00:00 2026-05-13T23:46:51+00:00

For debugging / performance tests I would like to dynamically add logging code to

  • 0

For debugging / performance tests I would like to dynamically add logging code to all event handlers of components of a given type at run time.

For example, for all Datasets in a Datamodule, I need to run code in the BeforeOpen and AfterOpen events to capture the start time, and to log the elapsed time in AfterOpen.

I would prefer to do this dynamically (no component subclassing), so that I can add this to all existing datamodules and forms with minimal effort only when needed.

Iterating all components and filtering by their type is easy, but for the components which already have event handlers assigned, I need a way to store the existing event handlers, and assign a new modified event handler which first does the logging and then will invoke the original code which was already present.

So this code

procedure TMyDatamodule.OnBeforeOpen(Sender: TDataset);
begin
  SomeProc;
end;

at run time would become

procedure TMyDatamodule.OnBeforeOpen(Sender: TDataset);
begin
  StoreStartTime(Sender); // injected code

  SomeProc;
end;

Is there a design pattern which can be applied, or even some example code which shows how to implement this in Delphi?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:46:52+00:00Added an answer on May 13, 2026 at 11:46 pm

    You can use the following scheme to rewire the datasets:

    type
      TDataSetEventWrapper = class
      private
        FDataSet: TDataSet;
        FOrgAfterOpen: TDataSetNotifyEvent;
        FOrgBeforeOpen: TDataSetNotifyEvent;
        procedure MyAfterOpen(DataSet: TDataSet);
        procedure MyBeforeOpen(DataSet: TDataSet);
      protected
        property DataSet: TDataSet read FDataSet;
      public
        constructor Create(ADataSet: TDataSet);
        destructor Destroy; override;
      end;
    
    constructor TDataSetEventWrapper.Create(ADataSet: TDataSet);
    begin
      Assert(ADataSet <> nil);
      inherited Create;
      FDataSet := ADataSet;
      FOrgAfterOpen := FDataSet.AfterOpen;
      FOrgBeforeOpen := FDataSet.BeforeOpen;
      FDataSet.AfterOpen := MyAfterOpen;
      FDataSet.BeforeOpen := MyBeforeOpen;
    end;
    
    destructor TDataSetEventWrapper.Destroy;
    begin
      FDataSet.AfterOpen := FOrgAfterOpen;
      FDataSet.BeforeOpen := FOrgBeforeOpen;
      inherited;
    end;
    
    procedure TDataSetEventWrapper.MyBeforeOpen(DataSet: TDataSet);
    begin
      if Assigned(FOrgBeforeOpen) then
        FOrgBeforeOpen(DataSet);
    end;
    
    procedure TDataSetEventWrapper.MyAfterOpen(DataSet: TDataSet);
    begin
      if Assigned(FOrgAfterOpen) then
        FOrgAfterOpen(DataSet);
    end;
    

    Inside MyAfterOpen and MyBeforeOpen you can bring in your code before, after or around the call to the original event handler.

    Collect the wrapper objects in a TObjectList with OwnsObjects := true and everything will revert to the original when you clear or free the objectlist.

    Caution: For this code to work the events have to be wired already when you create the wrappers and manually reassigning those events is forbidden.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 436k
  • Answers 436k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer When I increment versions like this I typically keep the… May 15, 2026 at 3:47 pm
  • Editorial Team
    Editorial Team added an answer As I wrote before to @mwilson, you cannot directly delete… May 15, 2026 at 3:47 pm
  • Editorial Team
    Editorial Team added an answer http://www.viara.eu/en/nvtcom.htm May 15, 2026 at 3:47 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.