West Wind Internet and Client Tools
Event Handling for wwDotnetBridge
Gravatar is a globally recognized avatar based on your email address. Event Handling for wwDotnetBridge
  Rick Strahl
  All
  Apr 16, 2018 @ 08:38pm

Hi all,

Here's an interesting discussion on the wwDotnetBridge Open Source library bits on GitHub in regards to adding support for .NET event handling to wwDotnetBridge. This was started by Edward Brey and while it's not all quite figured out, this looks very promising.

What this means is that we soon will be able to handle .NET events with wwDotnetBridge without having to implement an interface via COM. Previously the only way to get event handling to work required a FoxPro interface that is registered with COM and a .NET Object registered with COM. In the past my suggestion for event scenarios has been to create a .NET object in .NET code to handle the events and then have a proxy object that you pass in that can receive these events. Edward has basically figured out a way - via dynamic Expressions to create that proxy object on the fly, intercept these events and then forward it to an event handler of your choice. It's basically the same idea as the Event Implementation in COM had, except that you don't have to muck around with COM in any way. Instead you can just create an object that implements On<Event> methods with the event signature and you can receive events.

Here's an example that now works, basically hooking up the .NET FileSystemWatcher object that allows you to monitor file system changes (new, changed, deleted and renamed files in a folder or folder hierarchy):

do wwDotNetBridge
LOCAL loBridge as wwDotNetBridge
loBridge = GetwwDotnetBridge()

*** Set up the .NET FileWatcher
loFW = loBridge.CreateInstance("System.IO.FileSystemWatcher","C:\temp")
loFw.EnableRaisingEvents = .T.
loFw.IncludeSubdirectories = .T.

*** Set up FoxPro Event Handler - implemented below
loFwHandler = CREATEOBJECT("FwEventHandler")
loSubscription = loBridge.SubscribeToEvents(loFw, loFwHandler)


*** allow FileWatcher to start monitoring
DOEVENTS

*** Make some file ops that trigger the watcher
lcFile = "c:\temp\test.txt"
DELETE FILE ( lcFile )  
STRTOFILE("DDD",lcFile)
STRTOFILE("FFF",lcFile)

*** wait - you can go change files in the folder and they should output msgs
WAIT WINDOW

loSubscription.Dispose()

RETURN


*** Event handler implementation
*** Implement each event you want to handle as `On<Event>`
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

If you're interested you can follow the discussion or chime in, on the GitHub repo here:

This is the first real submission to wwDotnetBridge and it's a good one. Thanks to Edward Brey...

+++ Rick ---

© 1996-2024