In build 2.08 we have added the option to register an event handler that receives workarea events. This can be used to monitor access to workareas and for example write this information to a log file.
There are 2 ways to install this event handler:
1.You create a function/ method that implements the DbNotifyEventHandler delegate.
PUBLIC DELEGATE XSharp.DbNotifyEventHandler(osender AS XSharp.RDD.IRDD, e AS XSharp.DBNotifyEventArgs) AS VOID
2.You create a class that implements the IDbnotify interface, which only has a method named Notify() with the same prototype as the delegate from 1)
•Your event handler should do as little as possible if you don't want to slow down the whole RDD system.
•Do not manipulate any workareas from within your event handler to avoid recursion.
•Unregister your event handler as soon as possible.
•If you use an object for event handling you are responsible yourself to manage the life time of this object. And be sure to unregister the object before it runs out of scope
•In some cases (operations that work on more than one workarea, such as DbCommitAll() , DbUnLockAll() ) the event handler will not always receive a sender parameter. Be prepared for that !
•The events are sent from the CoreDb level. So if a method inside an RDD calls another method inside that RDD (for example CreateOrder() might call GoTop() after creating the order) then you will not see that second event, only the events related to the method that was called from the CoreDb() level, so the events related to the Ordercreation in this example.
If you use approach 1 then you need to add the event handler to CoreDb.Notify like in the following example:
FUNCTION NotifyRDDOperations(oRDD AS XSharp.RDD.IRdd, oEvent AS XSharp.DbNotifyEventArgs) AS VOID
IF oRDD != NULL
? oRDD:Alias, oEvent:Type:ToString(), oEvent:Data
ELSE
? "(no area)",oEvent:Type:ToString(), oEvent:Data
ENDIF
RETURN
FUNCTION Start() AS VOID
CoreDb.Notify += NotifyRDDOperations
? DbCreate("Test", {{"FLD1" , "C" , 10 , 0} }) // This will trigger a notification
CoreDb.Notify -= NotifyRDDOperations // Do not forget to unregister!
WAIT
RETURN
The parameters to the function are:
oRDD | The RDD for which the event is triggered. This may be NULL for events that involve multiple workareas, such as DbUnlockAll() |
oEvent | An event handler object that has 2 properties |
Type A Value of the DbNotificationType enum |
Data An object that has additional information about the event, such as the fieldname for a PieldPut and the recordnumber for a Append or Delete event.. |
For the second approach you create a class and register the class with the DbRegisterClient() function and unregister the class with the DbUnRegisterClient function
The method in the class will get the same arguments as the event handler function:
CLASS Notifier IMPLEMENTS IDbnotify
METHOD Notify(oRDD AS XSharp.RDD.IRdd, oEvent AS XSharp.DbNotifyEventArgs) AS VOID
IF oRDD != NULL
? oRDD:Alias, oEvent:Type:ToString(), oEvent:Data
ELSE
? "(no area)",oEvent:Type:ToString(), oEvent:Data
ENDIF
RETURN
END CLASS
FUNCTION Start() AS VOID
LOCAL oNot AS Notifier
oNot := Notifier{}
DbRegisterClient(oNot)
? DbCreate("Test", {{"FLD1" , "C" , 10 , 0} }) // This will trigger a notification
DbUnRegisterClient(oNot) // Do not forget to unregister!
WAIT
RETURN
DbNotificationType
DbRegisterClient()
DbUnRegisterClient
DbNotifyEventHandler
IDbnotify