के लिए marshalled किया गया था मैं क्वेरी उपकरणों के लिए 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.
मैं LoadDevices विधि की शुरुआत में
MessageBox.Show(Thread.CurrentThread.ManagedThreadId.ToString());
डाल के साथ अपवाद फेंकता है और मुझे लगता है कि यह हमेशा एक ही से कहा जाता है धागा (1)। क्या आप कृपया बता सकते हैं कि यहां क्या हो रहा है और इस त्रुटि से कैसे छुटकारा पाएं?