Note that there are some explanatory texts on larger screens.

plurals
  1. PODisconnectedContext was detected when using ManagementObjectSearcher
    primarykey
    data
    text
    <p>I am using the following function within a WndProc override:</p> <pre><code>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&amp;PID_1234") Then Return "COM" &amp; comPortNumber End If End If Next Catch ex As Exception End Try Return "" End Function </code></pre> <p>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 <code>For Each port As System...</code> line), I get this message:</p> <blockquote> <p><strong>DisconnectedContext was detected</strong></p> <p>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.</p> </blockquote> <p>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?</p> <hr> <p>I found <a href="http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/20f46b55-6ffe-4961-b301-636e062029b6/" rel="nofollow noreferrer">a post on MSDN outlining a very similar problem problem</a>. The guy says:</p> <blockquote> <p>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.</p> </blockquote> <p>I wish I could figure out what is different in a Forms application.</p> <hr> <p><strong>Edit and Possible Solution:</strong> Based on the answer at <a href="https://stackoverflow.com/questions/3921661/disconnectedcontext-mda-when-calling-wmi-functions-in-single-threaded-application/5657777#5657777">DisconnectedContext MDA when calling WMI functions in single-threaded application</a> I think the solution here is to call <code>GetPortName()</code> outside of WndProc. I will test this tonight and post my result.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload