diff --git a/Distribution/wwDotnetBridge.PRG b/Distribution/wwDotnetBridge.PRG index a129ef8..92e1af2 100644 --- a/Distribution/wwDotnetBridge.PRG +++ b/Distribution/wwDotnetBridge.PRG @@ -252,7 +252,7 @@ ENDFUNC *** Function: Unloads the CLR and AppDomain *** Assume: Don't call this unless you want to explicity force *** the AppDomain to be unloaded and a new one to be spun -*** up. Generally a single domain shoudl be sufficient. +*** up. Generally a single domain should be sufficient. *** Pass: *** Return: ************************************************************************ @@ -473,8 +473,7 @@ DO CASE ENDCASE IF loBridge.Error - this.cErrorMsg = loBridge.ErrorMessage - this.lError = .T. + this.SetError(loBridge.ErrorMessage) ENDIF RETURN loResult @@ -526,8 +525,7 @@ ENDCASE loResult = loBridge.InvokeMethod_ParameterArray(loObject, lcMethod, loArray) IF loBridge.Error - this.cErrorMsg = loBridge.ErrorMessage - this.lError = .T. + this.SetError(loBridge.ErrorMessage) ENDIF RETURN loResult @@ -789,8 +787,7 @@ DO CASE ENDCASE IF loBridge.Error - this.cErrorMsg = loBridge.ErrorMessage - this.lError = .T. + this.SetError(loBridge.ErrorMessage) ENDIF RETURN loResult @@ -1818,7 +1815,9 @@ FUNCTION Setup(loBridge, loSource, loHandler, lcPrefix) this.oBridge = loBridge this.oHandler = loHandler this.oPrefix = lcPrefix -this.oSubscriber = loBridge.CreateInstance("Westwind.WebConnection.EventSubscriber", loSource) +Private handler +handler = m.loHandler +this.oSubscriber = loBridge.CreateInstance("Westwind.WebConnection.EventSubscriber", loSource, m.lcPrefix, _Vfp) this.HandleNextEvent() ENDFUNC @@ -2017,7 +2016,7 @@ ENDFUNC *** Function: Unloads the CLR and AppDomain *** Assume: Don't call this unless you want to explicity force *** the AppDomain to be unloaded and a new one to be spun -*** up. Generally a single domain shoudl be sufficient. +*** up. Generally a single domain should be sufficient. *** Pass: *** Return: ************************************************************************ diff --git a/DotnetBridge/Utilities/EventSubscriber.cs b/DotnetBridge/Utilities/EventSubscriber.cs index d7207bd..2e134c1 100644 --- a/DotnetBridge/Utilities/EventSubscriber.cs +++ b/DotnetBridge/Utilities/EventSubscriber.cs @@ -16,18 +16,24 @@ namespace Westwind.WebConnection public sealed class EventSubscriber : IDisposable { private readonly object _source; - private readonly List _eventHandlers = new List(); + private readonly List _eventHandlers = new List(); private readonly ConcurrentQueue _raisedEvents = new ConcurrentQueue(); private TaskCompletionSource _completion = new TaskCompletionSource(); - public EventSubscriber(object source) + public EventSubscriber(object source, String prefix = "", dynamic vfp = null) { // Indicates that initially the client is not waiting. _completion.SetResult(null); // For each event, adds a handler that calls QueueInteropEvent. _source = source; - foreach (var ev in source.GetType().GetEvents()) { + foreach (var ev in source.GetType().GetEvents()) + { + // handler is a PRIVATE variable defined in EventSubscription.Setup(). + Boolean hasMethod = vfp?.Eval($"PEMSTATUS(m.handler, '{prefix}{ev.Name}', 5)") ?? true; + if (!hasMethod) + continue; + var eventParams = ev.EventHandlerType.GetMethod("Invoke").GetParameters().Select(p => Expression.Parameter(p.ParameterType)).ToArray(); var eventHandlerLambda = Expression.Lambda(ev.EventHandlerType, Expression.Call( @@ -38,15 +44,26 @@ public EventSubscriber(object source) eventParams); var eventHandler = eventHandlerLambda.Compile(); ev.AddEventHandler(source, eventHandler); - _eventHandlers.Add(eventHandler); + _eventHandlers.Add(new DelegateInfo(eventHandler, ev)); + } + } + + class DelegateInfo + { + public DelegateInfo(Delegate handler, EventInfo eventInfo) + { + Delegate = handler; + EventInfo = eventInfo; } + + public Delegate Delegate { get; } + public EventInfo EventInfo { get; } } public void Dispose() { - var events = _source.GetType().GetEvents(); - for (int e = 0; e < events.Length; ++e) - events[e].RemoveEventHandler(_source, _eventHandlers[e]); + foreach (var item in _eventHandlers) + item.EventInfo.RemoveEventHandler(_source, item.Delegate); _completion.TrySetCanceled(); } diff --git a/DotnetBridge/wwDotNetBridge.cs b/DotnetBridge/wwDotNetBridge.cs index 9a990fc..f89a2b2 100644 --- a/DotnetBridge/wwDotNetBridge.cs +++ b/DotnetBridge/wwDotNetBridge.cs @@ -279,7 +279,7 @@ protected internal object CreateInstance_Internal(string TypeName, params object if (type == null) { - SetError("Type not loaded. Please load call LoadAssembly first."); + SetError("Type not loaded. Please call LoadAssembly first."); return null; }