我正在使用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);
}
Run Code Online (Sandbox Code Playgroud)
此代码使用以下文本抛出异常
The application called an interface that was marshalled for a different thread.
我放
MessageBox.Show(Thread.CurrentThread.ManagedThreadId.ToString());
Run Code Online (Sandbox Code Playgroud)
在LoadDevices方法的开头,我看到它总是从同一个线程(1)调用.你能解释一下这里发生了什么,以及如何摆脱这个错误?
最后我用新线程解决了它。我拆分了这个方法,所以现在我有方法和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);
}
}
Run Code Online (Sandbox Code Playgroud)
当我收到 DBT_DEVICEARRIVAL 或 DBT_DEVICEREMOVECOMPLETE 消息时,我调用
ThreadPool.QueueUserWorkItem(s => InvokeLoadDevices());
Run Code Online (Sandbox Code Playgroud)
谢谢。