wwDotNetBridge::SubscribeToEvents

Allows you to capture events on a source object, by passing in a callback handler that maps the events of the target object with corresponding methods on the handler.

To handle events:

  • Create an Event Handler Object
    Create a Custom class that implements methods that match the events of the .NET object that fires events with a On<EventName> prefix. Each 'asdd' method's parameters should match the parameters of the .NET event delegate. You only need to implement the methods you want to listen to - other events are ignored.

  • Create an Event Subscription
    Call loBridge.SubscribeToEvents() which binds a .NET event source object to a FoxPro event handler.

  • Continue running your Application
    Events are handled asynchronously in .NET and run in the background. Your application continues running and as events fire in .NET, the On<Event> methods are fired on the Event Handler object in FoxPro.

  • Unsubscribe from the Event Subscription
    When you no longer want to listen to events, call loSubscription.UnSubscribe(). Make sure you do this before you exit FoxPro or you may crash VFP on shutdown.

o.SubscribeToEvents(loSource, loHandler, lcPrefix)

Return Value

Subscription class. Use .Unsubscribe()?on it to release event handlers.

Parameters

loSource
The object that has events we want to handle

loHandler
Class that implements each of the event methods using On<eventName> for the handler method name for each event. Each method's signature should match the event's parameters.

lcPrefix
Optional - event method prefix. Defaults to On

Remarks

Event handlers are fired from .NET Async code which can and often runs on seperate threads. COM marshals the thread back to FoxPro's STA execution thread, but it can fire at any time between commands, interrupting other code executing. For this reason ensure to keep the code executing short and you always reset state at the end of the call. If necessary capture the result values and save them, then delay execute the result code either in a known repeat location of your code or using the EvalTimer() class/function, which occurs at more predictable code points.

Example

CLEAR
do wwDotNetBridge
LOCAL loBridge as wwDotNetBridge
loBridge = CreateObject("wwDotNetBridge","V4")

*** .NET File System watcher notifies of changes in file system
loFW = loBridge.CreateInstance("System.IO.FileSystemWatcher","C:\temp")
loFw.EnableRaisingEvents = .T.
loFw.IncludeSubDirectories = .T.

*** Handler instance that maps events we want to capture
loFwHandler = CREATEOBJECT("FwEventHandler")
loSubscription = loBridge.SubscribeToEvents(loFw, loFwHandler)

DOEVENTS

lcFile = "c:\temp\test.txt"
DELETE FILE ( lcFile )  
STRTOFILE("DDD",lcFile)
STRTOFILE("FFF",lcFile)

WAIT WINDOW

loSubscription.Unsubscribe()

RETURN


*** Handler object implementation that maps the
*** event signatures for the events we want to handle
DEFINE CLASS FwEventHandler as Custom

FUNCTION OnCreated(sender,ev)
? "FILE CREATED: "
?  ev.FullPath
ENDFUNC

FUNCTION OnChanged(sender,ev)
? "FILE CHANGE: "
?  ev.FullPath
ENDFUNC

FUNCTION OnDeleted(sender, ev)
? "FILE DELETED: "
?  ev.FullPath
ENDFUNC

FUNCTION OnRenamed(sender, ev)
LOCAL lcOldPath, lcPath

? "FILE RENAMED: " 
loBridge = GetwwDotnetBridge()

lcOldPath = loBridge.GetProperty(ev,"OldFullPath")
lcPath = loBridge.GetProperty(ev,"FullPath")
? lcOldPath + " -> " + lcPath

ENDFUNC

ENDDEFINE

See also:

Class wwDotNetBridge

© West Wind Technologies, 1996-2022 • Updated: 06/09/18
Comment or report problem with topic