public ManagementEventWatcher(ManagementScope scope, EventQuery query, EventWatcherOptions options) { if (scope != null) { this.scope = ManagementScope._Clone(scope, new IdentifierChangedEventHandler(this.HandleIdentifierChange)); } else { this.scope = ManagementScope._Clone(null, new IdentifierChangedEventHandler(this.HandleIdentifierChange)); } if (query != null) { this.query = (EventQuery) query.Clone(); } else { this.query = new EventQuery(); } this.query.IdentifierChanged += new IdentifierChangedEventHandler(this.HandleIdentifierChange); if (options != null) { this.options = (EventWatcherOptions) options.Clone(); } else { this.options = new EventWatcherOptions(); } this.options.IdentifierChanged += new IdentifierChangedEventHandler(this.HandleIdentifierChange); this.enumWbem = null; this.cachedCount = 0; this.cacheIndex = 0; this.sink = null; this.delegateInvoker = new WmiDelegateInvoker(this); }
//******************************************** //Stop //******************************************** /// <summary> /// <para>Cancels the subscription whether it is synchronous or asynchronous.</para> /// </summary> public void Stop() { //For semi-synchronous, release the WMI enumerator to cancel the subscription if (null != enumWbem) { Marshal.ReleaseComObject(enumWbem); enumWbem = null; FireStopped (new StoppedEventArgs (options.Context, (int)ManagementStatus.OperationCanceled)); } // In async mode cancel the call to the sink - this will // unwind the operation and cause a Stopped message if (null != sink) { sink.Cancel (); sink = null; } }
//******************************************** //Start //******************************************** /// <summary> /// <para>Subscribes to events with the given query and delivers /// them, asynchronously, through the <see cref='System.Management.ManagementEventWatcher.EventArrived'/> event.</para> /// </summary> public void Start() { Initialize (); // Cancel any current event query Stop (); // Submit a new query SecurityHandler securityHandler = Scope.GetSecurityHandler(); IWbemServices wbemServices = scope.GetIWbemServices(); try { sink = new SinkForEventQuery(this, options.Context, wbemServices); if (sink.Status < 0) { Marshal.ThrowExceptionForHR(sink.Status); } // For async event queries we should ensure 0 flags as this is // the only legal value int status = scope.GetSecuredIWbemServicesHandler(wbemServices).ExecNotificationQueryAsync_( query.QueryLanguage, query.QueryString, 0, options.GetContext(), sink.Stub); if (status < 0) { if (sink != null) { sink.ReleaseStub(); sink = null; } if ((status & 0xfffff000) == 0x80041000) ManagementException.ThrowWithExtendedInfo((ManagementStatus)status); else Marshal.ThrowExceptionForHR(status); } } finally { securityHandler.Reset(); } }
/// <summary> /// <para> Initializes a new instance of the <see cref='System.Management.ManagementEventWatcher'/> class /// that listens for events conforming to the given WMI event query, according to the specified /// options. For this variant, the query and the scope are specified objects. The /// options object can specify options such as timeout and context information.</para> /// </summary> /// <param name='scope'>A <see cref='System.Management.ManagementScope'/> object representing the scope (namespace) in which the watcher will listen for events.</param> /// <param name=' query'>An <see cref='System.Management.EventQuery'/> object representing a WMI event query, which determines the events for which the watcher will listen.</param> /// <param name='options'>An <see cref='System.Management.EventWatcherOptions'/> object representing additional options used to watch for events. </param> public ManagementEventWatcher( ManagementScope scope, EventQuery query, EventWatcherOptions options) { if (null != scope) this.scope = ManagementScope._Clone(scope, new IdentifierChangedEventHandler(HandleIdentifierChange)); else this.scope = ManagementScope._Clone(null, new IdentifierChangedEventHandler(HandleIdentifierChange)); if (null != query) this.query = (EventQuery)query.Clone(); else this.query = new EventQuery(); this.query.IdentifierChanged += new IdentifierChangedEventHandler(HandleIdentifierChange); if (null != options) this.options = (EventWatcherOptions)options.Clone(); else this.options = new EventWatcherOptions(); this.options.IdentifierChanged += new IdentifierChangedEventHandler(HandleIdentifierChange); enumWbem = null; cachedCount = 0; cacheIndex = 0; sink = null; delegateInvoker = new WmiDelegateInvoker (this); }
public void Stop() { if (this.enumWbem != null) { Marshal.ReleaseComObject(this.enumWbem); this.enumWbem = null; this.FireStopped(new StoppedEventArgs(this.options.Context, 0x40006)); } if (this.sink != null) { this.sink.Cancel(); this.sink = null; } }
public void Start() { this.Initialize(); this.Stop(); SecurityHandler securityHandler = this.Scope.GetSecurityHandler(); IWbemServices iWbemServices = this.scope.GetIWbemServices(); try { this.sink = new SinkForEventQuery(this, this.options.Context, iWbemServices); if (this.sink.Status < 0) { Marshal.ThrowExceptionForHR(this.sink.Status); } int errorCode = this.scope.GetSecuredIWbemServicesHandler(iWbemServices).ExecNotificationQueryAsync_(this.query.QueryLanguage, this.query.QueryString, 0, this.options.GetContext(), this.sink.Stub); if (errorCode < 0) { if (this.sink != null) { this.sink.ReleaseStub(); this.sink = null; } if ((errorCode & 0xfffff000L) == 0x80041000L) { ManagementException.ThrowWithExtendedInfo((ManagementStatus) errorCode); } else { Marshal.ThrowExceptionForHR(errorCode); } } } finally { securityHandler.Reset(); } }
public void Start() { this.Initialize(); this.Stop(); SecurityHandler securityHandler = this.Scope.GetSecurityHandler(); IWbemServices wbemServices = this.scope.GetIWbemServices(); try { this.sink = new SinkForEventQuery(this, this.options.Context, wbemServices); if (this.sink.Status < 0) { Marshal.ThrowExceptionForHR(this.sink.Status); } int num = this.scope.GetSecuredIWbemServicesHandler(wbemServices).ExecNotificationQueryAsync_(this.query.QueryLanguage, this.query.QueryString, 0, this.options.GetContext(), this.sink.Stub); if (num < 0) { if (this.sink != null) { this.sink.ReleaseStub(); this.sink = null; } if (((long)num & (long)-4096) != (long)-2147217408) { Marshal.ThrowExceptionForHR(num); } else { ManagementException.ThrowWithExtendedInfo((ManagementStatus)num); } } } finally { securityHandler.Reset(); } }