使用ManagementObjectSearcher时检测到DisconnectedContext [英] DisconnectedContext was detected when using ManagementObjectSearcher
问题描述
我正在WndProc覆盖中使用以下功能:
I am using the following function within a WndProc override:
Public Function GetPortName() As String
Dim portNameData As String
Dim comPortNumber As String
Try
Dim portSearcher As New ManagementObjectSearcher("\root\CIMV2", "Select Name, PNPDeviceID from Win32_PnPEntity")
For Each port As System.Management.ManagementObject In portSearcher.Get()
If port("Name").ToString.ToUpper.Contains("(COM") Then
portNameData = port("Name").ToString
comPortNumber = port("Name").ToString.Substring(port("Name").ToString.IndexOf("(COM") + 4)
comPortNumber = comPortNumber.TrimEnd(")"c)
If port("PNPDeviceID").ToString.ToUpper.StartsWith("USB\VID_1234&PID_1234") Then
Return "COM" & comPortNumber
End If
End If
Next
Catch ex As Exception
End Try
Return ""
End Function
该函数工作正常,但是我现在正在通过表单在新的应用程序中使用它.在For循环的每次迭代中(在For Each port As System...
行上),我都会收到以下消息:
The function works fine, but I am using it in a new application now from a form. On each iteration of the For loop (on the For Each port As System...
line), I get this message:
检测到DisconnectedContext
上下文0x607fd8已断开连接.没有代理将用于为COM组件上的请求提供服务.这可能会导致损坏或数据丢失.为避免此问题,请确保所有上下文/公寓都保持活动状态,直到使用代表其中包含的COM组件的RuntimeCallableWrappers完全完成应用程序为止.
Context 0x607fd8 is disconnected. No proxy will be used to service the request on the COM component. This may cause corruption or data loss. To avoid this problem, please ensure that all contexts/apartments stay alive until the application is completely done with the RuntimeCallableWrappers that represent COM components that live inside them.
我认为这是某种线程问题.如何以一种不会发生此错误的方式从表单中调用此函数?
I think this is a threading issue of some sort. How can I call this function from a form in such a way that this error doesn't occur?
我找到了在MSDN上的一篇文章,概述了一个非常相似的问题.那个人说:
对不起,请不要浪费时间.在简单的控制台应用程序中运行时,该代码可以正常工作.在Windows窗体中调用它响应DriveDetectorEvent时出现了问题.
Sorry, don't waste your time. The code works fine when it's run in a simple console app. The problem arose when it was called in a Windows form responding to a DriveDetectorEvent.
我希望我能弄清楚Forms应用程序中的不同之处.
I wish I could figure out what is different in a Forms application.
Edit and Possible Solution: Based on the answer at DisconnectedContext MDA when calling WMI functions in single-threaded application I think the solution here is to call GetPortName()
outside of WndProc. I will test this tonight and post my result.
推荐答案
After reading the answer at DisconnectedContext MDA when calling WMI functions in single-threaded application it seems that the reason my code doesn't work is that it is being called from WndProc, blocking WndProc. This causes problems, as marshaling requires pumping messages... but I have blocked.
出于我的目的,只需在计时器上扫描即可.您可能必须找到其他解决方案,例如在另一个线程上异步调用它.
For my purposes, simply scanning on a timer works. You may have to find another solution, such as calling it asynchronously on another thread.
这篇关于使用ManagementObjectSearcher时检测到DisconnectedContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!