Home All Groups Group Topic Archive Search About

VS2003 - VS2005 -> DisconnectedContext was detected

Author
26 Oct 2006 4:30 PM
Ralf
Hi!

If I do the following in VS2003 it works - and in VS2005 it complains about
a DisconnectedContext...

using (ManagementClass mcDiskDrive = new ManagementClass(@"Win32_Diskdrive"))
{
                // mcDiskDrive.GetInstances() is thowing the exception
    foreach (ManagementObject moDiskDrive in mcDiskDrive.GetInstances())
    {
                }
}

Does anyone know what might go wrong?

Thanks
Ralf

Author
27 Oct 2006 10:21 AM
Ralf
Hi!

If I switch off the MDA (Debug/Exceptions -> Managed Debugging Assistants)
the Exception RPC_E_WRONG_THREAD shows up.

OK - I understand the problem, but what can I do to resolve it? How can I
marshall the objects into my thread space? Or do I have to do something
different ...?

Thanks in advance
Ralf

Show quote
"Ralf" wrote:

> Hi!
>
> If I do the following in VS2003 it works - and in VS2005 it complains about
> a DisconnectedContext...
>
> using (ManagementClass mcDiskDrive = new ManagementClass(@"Win32_Diskdrive"))
> {
>                 // mcDiskDrive.GetInstances() is thowing the exception
>     foreach (ManagementObject moDiskDrive in mcDiskDrive.GetInstances())
>     {
>                 }
> }
>
> Does anyone know what might go wrong?
>
> Thanks
> Ralf
Author
28 Oct 2006 4:04 PM
Ralf
Hi!
Sorry - my mistake was that I called this method from a message handler of
WndProc ... So I got this cross thread problem.

OK - I have to call Invoke, but now I get the exception: "This method is not
implemented ..." - but it should be there, at least I can call it directly
without problems.

Here is a sample - perhaps someone can direct me to a solution?

public class Form1 : Form
{
    public Form1()
    {
        ShowDrive();    // 1: is working
    }
    protected override void WndProc(ref Message m)
    {
        if(m.Msg == WM_DEVICECHANGE)
        {
            ShowDrive();    // 2: not working -> ShowDriveInvoke();
        }
        base.WndProc(ref m);
    }
    private void ShowDrive()
    {
        using (ManagementClass mc = new ManagementClass(@"Win32_Diskdrive"))
        {
            foreach (ManagementObject mo in mc.GetInstances())  // -> wrong
thread, clear so far ...
            {
                ...
            }
        }
    }
    private void ShowDriveInvoke()
    {
        using (ManagementClass mc = new ManagementClass(@"Win32_Diskdrive"))
        {
            // throws Exception -> "This method is not implemented in any
class"
            object[] args = {""};
            foreach (ManagementObject mo in mc.InvokeMethod("GetInstances",
args) as ManagementObjectCollection)
            {
                ...
            }
        }
    }
}

Thanks a lot

Ralf


Show quote
"Ralf" wrote:

> Hi!
>
> If I switch off the MDA (Debug/Exceptions -> Managed Debugging Assistants)
> the Exception RPC_E_WRONG_THREAD shows up.
>
> OK - I understand the problem, but what can I do to resolve it? How can I
> marshall the objects into my thread space? Or do I have to do something
> different ...?
>
> Thanks in advance
> Ralf
>
> "Ralf" wrote:
>
> > Hi!
> >
> > If I do the following in VS2003 it works - and in VS2005 it complains about
> > a DisconnectedContext...
> >
> > using (ManagementClass mcDiskDrive = new ManagementClass(@"Win32_Diskdrive"))
> > {
> >                 // mcDiskDrive.GetInstances() is thowing the exception
> >     foreach (ManagementObject moDiskDrive in mcDiskDrive.GetInstances())
> >     {
> >                 }
> > }
> >
> > Does anyone know what might go wrong?
> >
> > Thanks
> > Ralf
Author
29 Oct 2006 10:24 PM
Ralf
Hi!

OK - I must have been sleeping ...

public class Form1 : Form
{
    public delegate string DelegateGetDrives();

    public Form1()
    {
    }
    internal const int WM_DEVICECHANGE = 0x0219;
    protected override void WndProc(ref Message m)
    {
        if(m.Msg == WM_DEVICECHANGE)
        {
            DelegateGetDrives gdi = new DelegateGetDrives(GetDrives);
            IAsyncResult result = gdi.BeginInvoke(null,"");
            ShowDrive((string)gdi.EndInvoke(result));
        }
        base.WndProc(ref m);
    }
    private void GetDrives()
    {
    }
    ...
}

Sorry that I bothered you all with this question - but sometimes ...

Cheers Ralf

Show quote
"Ralf" wrote:

> Hi!
> Sorry - my mistake was that I called this method from a message handler of
> WndProc ... So I got this cross thread problem.

> OK - I have to call Invoke, but now I get the exception: "This method is not
> implemented ..." - but it should be there, at least I can call it directly
> without problems.
>
> Here is a sample - perhaps someone can direct me to a solution?
>
> public class Form1 : Form
> {
>     public Form1()
>     {
>         ShowDrive();    // 1: is working
>     }
>     protected override void WndProc(ref Message m)
>     {
>         if(m.Msg == WM_DEVICECHANGE)
>         {
>             ShowDrive();    // 2: not working -> ShowDriveInvoke();
>         }
>         base.WndProc(ref m);
>     }
>     private void ShowDrive()
>     {
>         using (ManagementClass mc = new ManagementClass(@"Win32_Diskdrive"))
>         {
>             foreach (ManagementObject mo in mc.GetInstances())  // -> wrong
> thread, clear so far ...
>             {
>                 ...
>             }
>         }
>     }
>     private void ShowDriveInvoke()
>     {
>         using (ManagementClass mc = new ManagementClass(@"Win32_Diskdrive"))
>         {
>             // throws Exception -> "This method is not implemented in any
> class"
>             object[] args = {""};
>             foreach (ManagementObject mo in mc.InvokeMethod("GetInstances",
> args) as ManagementObjectCollection)
>             {
>                 ...
>             }
>         }
>     }
> }
>
> Thanks a lot
>
> Ralf
>
>
> "Ralf" wrote:
>
> > Hi!
> >
> > If I switch off the MDA (Debug/Exceptions -> Managed Debugging Assistants)
> > the Exception RPC_E_WRONG_THREAD shows up.
> >
> > OK - I understand the problem, but what can I do to resolve it? How can I
> > marshall the objects into my thread space? Or do I have to do something
> > different ...?
> >
> > Thanks in advance
> > Ralf
> >
> > "Ralf" wrote:
> >
> > > Hi!
> > >
> > > If I do the following in VS2003 it works - and in VS2005 it complains about
> > > a DisconnectedContext...
> > >
> > > using (ManagementClass mcDiskDrive = new ManagementClass(@"Win32_Diskdrive"))
> > > {
> > >                 // mcDiskDrive.GetInstances() is thowing the exception
> > >     foreach (ManagementObject moDiskDrive in mcDiskDrive.GetInstances())
> > >     {
> > >                 }
> > > }
> > >
> > > Does anyone know what might go wrong?
> > >
> > > Thanks
> > > Ralf

AddThis Social Bookmark Button