未处理InvalidComObjectException [英] InvalidComObjectException was unhandled

查看:124
本文介绍了未处理InvalidComObjectException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在关闭表单时发生错误= 未处理InvalidComObjectException;
与其下层RCW分开的COM对象无法使用.


On Closing the form an error occures = InvalidComObjectException was unhandled ;
COM object that has been separated from its underlying RCW cannot be used.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;
namespace convertusbdetect
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }
      public   class USBInsertRemoveDetect : IDisposable
        {
            String ui;
            string ji;
        public delegate void EventArrivedDelegate(object sender, EventArrivedEventArgs e);
        public event EventArrivedDelegate EventArrived;
        ManagementEventWatcher mwatcher = null;
        public void Start()
        {
                WqlEventQuery query;
                ManagementScope scope = new ManagementScope("root\\CIMV2");
                scope.Options.EnablePrivileges = true;
                query = new WqlEventQuery();
                query.EventClassName = "__InstanceOperationEvent";
                query.WithinInterval = new TimeSpan(0, 0, 0, 0, 500);
                query.Condition = @"TargetInstance ISA ''Win32_USBControllerDevice''";
                mwatcher = new ManagementEventWatcher(scope, query);
                mwatcher.EventArrived += new EventArrivedEventHandler(UsbEventArrived);
                mwatcher.Start();

        }
       public    void UsbEventArrived(object sender, EventArrivedEventArgs e)
        {
                foreach (PropertyData pd in e.NewEvent.Properties)
                {
                    ManagementBaseObject mbo = null;
                    if ((mbo = pd.Value as ManagementBaseObject) != null)
                    {
                        foreach (PropertyData prop in mbo.Properties)
                        {
                            ui += prop.Value;
                            string[] splitDeviceId = ui.Split(''\\'');
                            string[] serialArray;
                            String serial;
                            String serial1;
                            int arrayLen = splitDeviceId.Length - 1;
                            serialArray = splitDeviceId[arrayLen].Split(''&'');
                            serial = serialArray[0];
                            if (serial == "3")
                            {
                                ji = serial;
                                System.Threading.Thread.Sleep(1);
                            }
                            else if (serial == "6")
                            {
                                ji = serial;
                                System.Threading.Thread.Sleep(1);
                            }
                            else if (serial == "7")
                            {
                                ji = serial;
                                System.Threading.Thread.Sleep(1);
                            }
                            else if (serial != ji)
                            {
                                if (serial.Contains(''"''))
                                {
                                    System.Threading.Thread.Sleep(1);
                                }
                                else
                                {
                                    ji = serial;
                                    if (serial == "")
                                    {
                                    }
                                    else
                                    {
                                        serial1 = serial;
                                        if (serial == serial1)
                                        {
                                            MessageBox.Show(serial1);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

        #region IDisposable Members
        public void Dispose()
        {
            mwatcher.Dispose();
        }
        #endregion
    }


        private void Form1_Load(object sender, EventArgs e)
        {
            USBInsertRemoveDetect loaddevice = new USBInsertRemoveDetect();
            loaddevice.Start ();
        }


    }
}

推荐答案

在哪里停止对设置的WMI事件的订阅?我在任何地方都看不到它,这可能是您收到此错误的原因. WMI事件涉及非托管资源,这些资源在超出范围时不会自动清除.您必须在应用关闭之前明确停止事件观察器并处置对象.
Where do you Stop your subscription to the WMI event you setup? I don''t see it anywhere and this is probably why your getting this error. WMI Events involve unmanaged resources that do not get cleaned up automatically when they go out of scope. You have to explicitly Stop your event watcher and Dispose of the object before your app closes.


这篇关于未处理InvalidComObjectException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆