예제 #1
0
        /// <summary>
        /// Starts the monitoring.
        /// </summary>
        internal void StartMonitoring(VirtualComPortsFinderMode monitorMode)
        {
            // Clear any existing monitoring if it exists
            StopMonitoring();

            // Add subscriptions to WMI insert and removal events
            WqlEventQuery   eventQuery;
            ManagementScope scope = new ManagementScope("root\\CIMV2");

            scope.Options.EnablePrivileges = true;

            try
            {
                if (monitorMode == VirtualComPortsFinderMode.Added || monitorMode == VirtualComPortsFinderMode.Both)
                {
                    // Subscribe to instance creation events
                    eventQuery = new WqlEventQuery("__InstanceCreationEvent");
                    eventQuery.WithinInterval       = new TimeSpan(0, 0, 3);
                    eventQuery.Condition            = @"TargetInstance ISA 'Win32_SerialPort'";
                    _usbInsertWatcher               = new ManagementEventWatcher(scope, eventQuery);
                    _usbInsertWatcher.EventArrived += UsbInsertWatcher_EventArrived;
                    _usbInsertWatcher.Start();
                    Trace.TraceInformation("{0}.StartMonitoring: Win32_SerialPort insert watcher started", CLASSNAME);
                }

                if (monitorMode == VirtualComPortsFinderMode.Removed || monitorMode == VirtualComPortsFinderMode.Both)
                {
                    // Subscribe to instance removal events
                    eventQuery = new WqlEventQuery("__InstanceDeletionEvent");
                    eventQuery.WithinInterval        = new TimeSpan(0, 0, 3);
                    eventQuery.Condition             = @"TargetInstance ISA 'Win32_SerialPort'";
                    _usbRemovalWatcher               = new ManagementEventWatcher(scope, eventQuery);
                    _usbRemovalWatcher.EventArrived += UsbRemovalWatcher_EventArrived;
                    _usbRemovalWatcher.Start();
                    Trace.TraceInformation("{0}.StartMonitoring: Win32_SerialPort removal watcher started", CLASSNAME);
                }
            }
            catch (Exception ex)
            {
                // Make sure we cleanup properly if there is an error
                Trace.TraceError("{0}.StartMonitoring: Exception={1}", CLASSNAME, ex.GetBaseException());
                StopMonitoring();
                throw;
            }
        }
        /// <summary>
        /// Starts the monitoring.
        /// </summary>
        /// <param name="monitorMode">The monitor mode.</param>
        public void StartMonitoring(VirtualComPortsFinderMode monitorMode)
        {
            // Clear any existing monitoring if it exists
            this.StopMonitoring();

            // Add subscriptions to WMI insert and removal events
            WqlEventQuery eventQuery;
            ManagementScope scope = new ManagementScope("root\\CIMV2");
            scope.Options.EnablePrivileges = true;

            try
            {
                if (monitorMode == VirtualComPortsFinderMode.Added || monitorMode == VirtualComPortsFinderMode.Both)
                {
                    // Subscribe to instance creation events
                    eventQuery = new WqlEventQuery("__InstanceCreationEvent");
                    eventQuery.WithinInterval = new TimeSpan(0, 0, 3);
                    eventQuery.Condition = @"TargetInstance ISA 'Win32_SerialPort'";
                    this.usbInsertWatcher = new ManagementEventWatcher(scope, eventQuery);
                    this.usbInsertWatcher.EventArrived += this.UsbInsertWatcher_EventArrived;
                    this.usbInsertWatcher.Start();
                    Trace.TraceInformation("{0}.StartMonitoring: Win32_SerialPort insert watcher started", CLASSNAME);
                }

                if (monitorMode == VirtualComPortsFinderMode.Removed || monitorMode == VirtualComPortsFinderMode.Both)
                {
                    // Subscribe to instance removal events
                    eventQuery = new WqlEventQuery("__InstanceDeletionEvent");
                    eventQuery.WithinInterval = new TimeSpan(0, 0, 3);
                    eventQuery.Condition = @"TargetInstance ISA 'Win32_SerialPort'";
                    this.usbRemovalWatcher = new ManagementEventWatcher(scope, eventQuery);
                    this.usbRemovalWatcher.EventArrived += this.UsbRemovalWatcher_EventArrived;
                    this.usbRemovalWatcher.Start();
                    Trace.TraceInformation("{0}.StartMonitoring: Win32_SerialPort removal watcher started", CLASSNAME);
                }
            }
            catch (Exception ex)
            {
                // Make sure we cleanup properly if there is an error
                Trace.TraceError("{0}.StartMonitoring: Exception={1}", CLASSNAME, ex.GetBaseException());
                this.StopMonitoring();
                throw;
            }
        }