该应用程序调用了一个为不同线程编组的接口

时间:2012-08-31 07:40:49

标签: c# .net com wmi

我正在使用WMI查询设备。我需要在插入或删除新设备时更新UI(为了使设备列表保持最新)。

private void LoadDevices()
{
    using (ManagementClass devices = new ManagementClass("Win32_Diskdrive"))
    {
        foreach (ManagementObject mgmtObject in devices.GetInstances())
        {
            foreach (ManagementObject partitionObject in mgmtObject.GetRelated("Win32_DiskPartition"))
            {
                foreach (ManagementBaseObject diskObject in partitionObject.GetRelated("Win32_LogicalDisk"))
                {
                    trvDevices.Nodes.Add( ... );
                }
            }
        }
    }
}

protected override void WndProc(ref Message m)
{
    const int WM_DEVICECHANGE = 0x0219;
    const int DBT_DEVICEARRIVAL = 0x8000;
    const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
    switch (m.Msg)
    {
        // Handle device change events sent to the window
        case WM_DEVICECHANGE:
            // Check whether this is device insertion or removal event
            if (
                (int)m.WParam == DBT_DEVICEARRIVAL ||
                (int)m.WParam == DBT_DEVICEREMOVECOMPLETE)
        {
            LoadDevices();
        }

        break;
    }

    // Call base window message handler
    base.WndProc(ref m);
}

此代码使用以下文本抛出异常

The application called an interface that was marshalled for a different thread.

我把

MessageBox.Show(Thread.CurrentThread.ManagedThreadId.ToString());

在LoadDevices方法的开头,我看到它总是从同一个线程(1)调用。你能解释一下这里发生了什么以及如何摆脱这个错误吗?

2 个答案:

答案 0 :(得分:2)

最后我用新线程解决了它。 我拆分了这个方法,现在我有GetDiskDevices()LoadDevices(List<Device>)方法,我有InvokeLoadDevices()方法。

private void InvokeLoadDevices()
{
    // Get list of physical and logical devices
    List<PhysicalDevice> devices = GetDiskDevices();

    // Check if calling this method is not thread safe and calling Invoke is required
    if (trvDevices.InvokeRequired)
    {
        trvDevices.Invoke((MethodInvoker)(() => LoadDevices(devices)));
    }
    else
    {
        LoadDevices(devices);
    }
}

当我收到DBT_DEVICEARRIVAL或DBT_DEVICEREMOVECOMPLETE消息时,我致电

ThreadPool.QueueUserWorkItem(s => InvokeLoadDevices());

感谢。

答案 1 :(得分:0)

对于W10上的UWP,您可以使用:

{{1}}
相关问题