/// <summary> /// Creates a new WMIInstanceWatcher with the specified poll interval. /// </summary> /// <param name="pollInterval">Polling interval between checks for new events</param> public WMIInstanceWatcher(TimeSpan pollInterval) { var wmiClassName = WMIUtils.GetWMIClassName <T>(); var condition = string.Format("(TargetInstance ISA '{0}')", wmiClassName); var createEventQuery = new WqlEventQuery { EventClassName = "__InstanceCreationEvent", WithinInterval = pollInterval, Condition = condition }; var deleteEventQuery = new WqlEventQuery { EventClassName = "__InstanceDeletionEvent", WithinInterval = pollInterval, Condition = condition }; var createWatcher = new ManagementEventWatcher(); createWatcher.EventArrived += HandleCreateEvent; createWatcher.Query = createEventQuery; var deleteWatcher = new ManagementEventWatcher(); deleteWatcher.EventArrived += HandleDeleteEvent; deleteWatcher.Query = deleteEventQuery; Watchers.Add(createWatcher); Watchers.Add(deleteWatcher); }
private void HandleEvent(object sender, EventArrivedEventArgs args) { if (EventOccurred == null) { return; } T instance = WMIUtils.FromEvent <T>(args); EventOccurred(instance); }
private void HandleDeleteEvent(object sender, EventArrivedEventArgs args) { if (InstanceDeleted == null) { return; } var obj = args.NewEvent.GetPropertyValue("TargetInstance"); T instance = WMIUtils.FromManagementObject <T>(obj as ManagementBaseObject); InstanceCreated(instance); }
/// <summary> /// Constructs a new WMIWatcher that watches for WMI events related to the WMI class specified by the type parameter. /// </summary> public WMIEventWatcher() { var wmiClassName = WMIUtils.GetWMIClassName <T>(); var query = string.Format("SELECT * FROM {0}", wmiClassName); var watcher = new ManagementEventWatcher(); watcher.EventArrived += HandleEvent; watcher.Query = new WqlEventQuery(query); Watchers.Add(watcher); }