Esempio n. 1
0
        internal void AddBinding(PSVariable variable, EventInfo eventInfo, string queueName)
        {
            Guid bindindID = Guid.NewGuid();

            lock (s_bindings)
            {
                s_bindings.Add(bindindID, new WeakReference(variable));
                WriteDebug("Added PSVariable tracking entry.");
            }

            try
            {
                Delegate handler = PSEventHelper.BindEventSink(variable, eventInfo, bindindID, queueName);
                WriteDebug("Event sink bound.");

                PSEventBindingAttribute attr = new PSEventBindingAttribute(eventInfo.Name, handler, bindindID);
                variable.Attributes.Add(attr);
                WriteDebug("Added binding attribute to PSVariable.");
            }
            catch (Exception ex)
            {
                m_command.WriteWarning(String.Format("Error binding to {0} on {1}: {2}", eventInfo.Name, variable.Name, ex.Message));

                s_bindings.Remove(bindindID);
                WriteDebug("Removed PSVariable tracking entry.");
                throw;
            }
        }
Esempio n. 2
0
        internal void RemoveBinding(PSVariable variable, string eventName)
        {
            // need for instead of foreach (need to modify collection)
            for (int index = 0; index < variable.Attributes.Count; index++)
            {
                PSEventBindingAttribute bindingAttr = variable.Attributes[index] as PSEventBindingAttribute;
                if (bindingAttr != null)
                {
                    if (bindingAttr.EventName.Equals(eventName, StringComparison.OrdinalIgnoreCase))
                    {
                        PSEventHelper.UnbindEventSink(variable, eventName, bindingAttr.Handler);
                        WriteDebug("Unbound event sink from variable.");

                        variable.Attributes.Remove(bindingAttr);
                        WriteDebug("Removed binding attribute from PSVariable.");

                        // remove PSVariable tracker
                        lock (s_bindings)
                        {
                            s_bindings.Remove(bindingAttr.BindingID);
                            WriteDebug("Removed PSVariable tracking entry.");
                        }
                    }
                }
            }
        }
        private void UnhookEventHandler()
        {
            Debug.WriteLine("Enter: " + m_eventName, "PSEventBindingAttribute.UnhookEventHandler");

            try
            {
                PSEventHelper.UnbindEventSinkFromTarget(m_initialTarget, m_eventName, m_handler);

                string message = String.Format("Removed event handler for '{0}' from {1}", m_eventName, m_initialTarget.GetType());
                Debug.WriteLine(message, "PSEventBindingAttribute.UnhookEventHandler");
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex, "PSEventBindingAttribute.UnhookEventHandler");
            }

            Debug.WriteLine("Exit: " + m_eventName, "PSEventBindingAttribute.UnhookEventHandler");
        }