A Generic TLKEventListener for LKSL

While playing a bit with Simon Stuarts LKSL event engine, I thought it would be quite handy to have a generic TLKEventListener taking away the need for some boiler plate code. Here it is:

 

 

About Uwe Raabe

Addicted to Pascal/Delphi since the late 70's
This entry was posted in Delphi, Programming. Bookmark the permalink.

8 Responses to A Generic TLKEventListener for LKSL

  1. A. Bouchez says:

    IMHO you should better keep your lock as small as possible, and use a local variable in the following method:

     
    procedure TLKEventListener<T>.EventCall(const AEvent: TLKEvent);
    var callback: TEventCallback;
    begin
      inherited;
      Lock; // We engage the Lock to prevent anything changing the value of “FOnEvent”
      try

        callback:= FOnEvent;
     finally
        Unlock; // Must not forget to UNLOCK when we’re done.
      end;

        if Assigned(callback) then
          callback(T(AEvent));
     end;

    • Uwe Raabe says:

      Thanks for the suggestion. As the code is quite similar to that of GetOnEvent, I implemented it slightly differently.

    • This is particularly important if the callback ends up trying to replace the OnEvent property of that listener in a “deferred manner” (I.E. from a different thread)… otherwise that Callback will enter an effective deadlock with the Listener.

  2. Hi Uwe,

    Would you object to me integrating this into the LKSL itself (obviously crediting you for the code and placing a link to your site here in the comment block)?

  3. Sir Rufo says:

    It would also be very nice to have a bridge to http://docwiki.embarcadero.com/Libraries/de/System.Messaging

    There are a lot of useful messages you can receive from there, but it is very unhandy to have to deal with several different messengers.

    For me I have build an extended version of the TMessageMangager (TMessageManagerEx) using generics, synchronize, etc. that will seamlessly integrate the standard TMessageManager.

    • The LKSL Event Engine was built SPECIFICALLY to work with its special “High Precision Event Threads” as a means of producing asynchronous, multi-threaded applications (where information, which in a sense acts as an instruction, is communicated between threads using the Event Engine).

      It’s not intended as a UI messaging system, or to “compete” with any of Delphi’s internal features… merely as a critical part of a new development methodology.

      Also, it was designed originally for my game engine… but once I realized it had a variety of potential purposes, I decided to put it out there as a part of my standard library.

  4. For the benefit of future readers, I have syndicated this awesome little feature into the LKSL repository 🙂

Comments are closed.