Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
| Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 27th, 2003, 11:30 PM
Registered User
 
Join Date: Jul 2003
Location: Lafayette, in, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Unhandled Exception:

How do you handle this exception?

Unhandled Exception: System.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable. at System.Runtime.InteropServices.Marshal.ThrowExcept ionForHR(Int32 errorCode, IntPtr errorInfo) at System.Runtime.InteropServices.Marshal.ThrowExcept ionForHR(Int32 errorCode) at System.Management.ManagementScope.InitializeGuts()

The try/catch block in the following code fragment should handle the above exception but does not. Why?

string[] systems = {"cpu1", "cpu2", "OnLine-
    NotResponding", "off-line"};
ConnectionOptions wmiConnOption;
ManagementObjectSearcher osSearch;
ManagementObjectCollection osInfo;
ManagementScope scope;
ObjectQuery osQuery;
wmiConnOption = new ConnectionOptions();
wmiConnOption.Impersonation = ImpersonationLevel.Impersonate;
osQuery = new ObjectQuery("Select * from Win32_OperatingSystem");
for (int i = 0; i < systems.Length; i++)
{
   scope = new ManagementScope("\\\\" + systems[i] + "\\root\\cimv2",
      wmiConnOption);
   osSearch = new ManagementObjectSearcher(scope, osQuery);
   try
   {
       scope.Connect();
   }
   catch(Exception e)
   {
       Console.WriteLine("Access error: " + e.Source);
   }
   if(scope.IsConnected)
   {
       osInfo = osSearch.Get();
       foreach(ManagementObject osItem in osInfo)
       {
          Console.WriteLine(systems[i] + " OS Version: " + osItem
             ["Version"]);
       }
   }
}

The security context is administrator for the code
and access to the remote systems. For systems "cpu1" & "cpu2" the
code works as expected. System "OnLine-NotResponding" can be
pinged but does not responding to remote access. System "Off-line"
is off-line or may not exist.

The last two systems in the array throw the "Unhandled Exception".

Thanks,
Mike
  #2 (permalink)  
Old July 28th, 2003, 09:20 PM
Registered User
 
Join Date: Jul 2003
Location: Lafayette, in, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is the complete test app.
The program works if you remove "[STAThread]"
from the code. What does "[STAThread]" do?


using System;
using System.Management;
using System.Collections;

namespace ConnectErr
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
             //[STAThread] * remove and code works. Why ????
        static void Main(string[] args)
        {
            string[] systems = {"cpu1", "cpu1", "OnLine-NotResponding", "bogusname"};
            ConnectionOptions wmiConnOption;
            ManagementObjectSearcher osSearch;
            ManagementObjectCollection osInfo;
            ManagementScope scope;
            ObjectQuery osQuery;
            wmiConnOption = new ConnectionOptions();
            wmiConnOption.Impersonation = ImpersonationLevel.Impersonate;
            osQuery = new ObjectQuery("Select * from Win32_OperatingSystem");
            for (int i = 0; i < systems.Length; i++)
            {
                scope = new ManagementScope("\\\\" + systems[i] + "\\root\\cimv2", wmiConnOption);
                osSearch = new ManagementObjectSearcher(scope, osQuery);
                try
                {
                    scope.Connect();
                }
                catch(System.Runtime.InteropServices.COMException e)
                {
                    Console.WriteLine("Access error: " + e.Source);
                }
                if(scope.IsConnected)
                {
                    osInfo = osSearch.Get();
                    foreach(ManagementObject osItem in osInfo)
                    {
                        Console.WriteLine(systems[i] + " OS Version: " + osItem["Version"]);
                    }
                }
            }
            Console.ReadLine();
        }
    }
}


- Mike
  #3 (permalink)  
Old July 29th, 2003, 08:15 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

Hi Mike

A query about STAThread has already been answered at
http://p2p.wrox.com/topic.asp?TOPIC_ID=426

Regards

Ankur Verma
.Net and C++ Specialist
Wiley Tech Support
  #4 (permalink)  
Old August 1st, 2003, 05:25 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to merediths
Default


The [STAThread] Attribute indicates that the code is Single Thread Apartment, or only one thread can be executing the code at a time.

Regards,
Meredtih Shaebanyan



Similar Threads
Thread Thread Starter Forum Replies Last Post
unhandled exception dipakp ASP.NET 2.0 Professional 1 July 6th, 2007 02:16 PM
Null reference exception was unhandled labby C# 2005 0 April 24th, 2007 06:14 AM
Unhandled NullReference Exception dheeraj1053 C# 1 May 30th, 2006 05:55 PM
Unhandled NullReference Exception MAKO C# 1 April 19th, 2006 05:46 AM
Unhandled Exception stu9820 ASP.NET 1.0 and 1.1 Basics 11 January 14th, 2004 05:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.