예제 #1
0
        /// <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);
        }
예제 #2
0
        private void HandleEvent(object sender, EventArrivedEventArgs args)
        {
            if (EventOccurred == null)
            {
                return;
            }
            T instance = WMIUtils.FromEvent <T>(args);

            EventOccurred(instance);
        }
예제 #3
0
        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);
        }
예제 #4
0
        /// <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);
        }