Exemplo n.º 1
0
        internal void StatusThread()
        {
            // Ask the system to notify our message queue when devices of
            // the indicated class are added or removed.
            IntPtr h = RequestDeviceNotifications(
                devClass.ToGuid().ToByteArray(),
                mq.Handle, fAll);

            // Wait for the queue to be signaled.  When it is, decode
            // the message and call any listeners for it.  If the
            // queue closes suddenly, that's our cue to exit, shutting
            // down notifications before we leave.
            PointToPointMsgQueue q = mq;

            while (mq != null)                  // Check the global object here.
            {
                // Wait.  On return, true indicates that the queue is
                // signaled; false is a chance to check whether we should
                // exit.
                if (q.Wait(-1))
                {
                    // Read the event data.
                    int bytes = 0;
                    PointToPointMsgQueueFlags readFlags = 0;
                    DEVDETAIL devDetail = new DEVDETAIL();
                    if (q.ReadMsgQueue(devDetail.getBytes(),
                                       DEVDETAIL.MAX_DEVDETAIL_SIZE,
                                       ref bytes, -1, ref readFlags))
                    {
                        // Handle the event.
                        OnDeviceNotification(new DeviceNotificationArgs(
                                                 devDetail.guidDevClass, devDetail.fAttached,
                                                 devDetail.szName));
                    }
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine(this, "mq is null in monitoring thread.  Exiting...");
                }
            }

            System.Diagnostics.Debug.WriteLine(this, "exiting DeviceStatusMonitor thread.");

            // Stop notifications to us.
            StopDeviceNotifications(h);
        }