Stackoverflow calling ManagementScope.Connect ();

Error receiving:

An unhandled exception of type 'System.StackOverflowException' occurred in System.Management.dll

My column:

[controlled by root transition]
System.Management.dll! System.Management.ManagementScope.InitializeGuts (object o) + 0x1a3 bytes

System.Management.dll! System.Management.ManagementScope.Initialize () + 0xa3 bytes
System.Management.dll! System.Management.ManagementScope.Connect () + 0x5 bytes
Computer_Managerment.exe! Computer_Managerment.WMI.ComputerInformation.ComputerInformation (string ComputerName = "pc357", string UserName = ", string Password =" ") String 228 + 0xd bytes C #
Computer_Managerment.exe! Computer_Managerment.ScanAllComputers.Workerthread () String 95 + 0x1e bytes C #
mscorlib.dll! System.Threading.ThreadHelper.ThreadStart_Context (state object) + 0x66 bytes
mscorlib.dll! System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext ExecutionContext, System.Threading.ContextCallback callback, object state) + 0x6f bytes
mscorlib.dll! System.Threading.ThreadHelper.ThreadStart () + 0x44 bytes

Code where I get stackoverflow:

try { gManager = new ManagementScope(ConnectStr, oConn); //\\\\" + gManager.Connect(); // This is where the crash happens } catch (UnauthorizedAccessException) { // Code removed } 

Basically this code works at this stage 1) I have a list of all computers in AD (more than 1k)
2) I have 10 threads rotating in a while loop, getting the compiler from the Que list.
3) When they have a computer name, they instantiate the ComputerInformation class, which runs gManager.Connect (); this is a failure again.

His understanding is that this crash / stackoverflow is happening inside its own code, but I assume that I am doing something wrong. If you need any more code, I would like to publish it.

Pr request more code: this is the code in which the workers live (normal for about 10 people)

 internal struct stWorkList { public Queue<string> Work; public List<ComputerInformation> CompInfo; public int FailedComputers; public int FailedPingCheck; public int SuccessComputers; public int TotalComputers; public int FailedAuth; public int FailedToContactWMIServer; } stWorkList gWorkList; void Workerthread() { Monitor.Enter(gUserName); Monitor.Enter(gPassword); string UserName = gUserName; string Password = gPassword; Monitor.Exit(gPassword); Monitor.Exit(gUserName); while (true) { Monitor.Enter(gWorkList.Work); if (gWorkList.Work.Count == 0) { Monitor.Exit(gWorkList.Work); break; } string ComputerName = gWorkList.Work.Dequeue(); Monitor.Exit(gWorkList.Work); if (ComputerName == null) continue; ComputerInformation iCI = new ComputerInformation(ComputerName, UserName, Password); Monitor.Enter(gWorkList.CompInfo); gWorkList.CompInfo.Add(iCI); switch (iCI.Status) { case eComputerCheckStatus.Connected: gWorkList.SuccessComputers++; break; case eComputerCheckStatus.FailedPingTest: gWorkList.FailedPingCheck++; gWorkList.FailedComputers++; break; case eComputerCheckStatus.UnauthorizedAccessException: gWorkList.FailedComputers++; gWorkList.FailedAuth++; break; case eComputerCheckStatus.FailedToContactWMIService: gWorkList.FailedToContactWMIServer++; gWorkList.FailedComputers++; break; case eComputerCheckStatus.UnkownFailed: gWorkList.FailedComputers++; break; } Monitor.Exit(gWorkList.CompInfo); iCI.Dispose(); } } 

Constructor in class ComputerInformation

 public ComputerInformation(string ComputerName, string UserName, string Password) { gComputerName = ComputerName; gHardDriveList = new List<stHarddiskInfo>(); gProccessInfo = new List<stProccessInfo>(); gCPUInfo = new List<stCPUInformation>(); gOSInfo = new stOSInfo(); gMemoryInfo = new List<stMemoryInfo>(); gPreformanceMemory = new stPreformanceMemory(); gProccessOverView = new stProccessOverview(); gMonitorInfo = new List<stMonitorInfo>(); gNetworkInfo = new List<stNetworkInfo>(); netMon = new Ping(); PingResponse response = netMon.PingHost(ComputerName, 1); if (response == null || response.PacketsReceived == 0) { gStatus = eComputerCheckStatus.FailedPingTest; gHasError = true; return; } gComputerIP = response.ServerEndPoint.Address.ToString(); ConnectionOptions oConn = new ConnectionOptions(); oConn.Timeout = new TimeSpan(0, 0, 10); if (!string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(UserName)) { oConn.Username = UserName; oConn.Password = Password; } string ConnectStr = "\\\\" + ComputerName + "\\root\\cimv2"; try { gManager = new ManagementScope(ConnectStr, oConn); //\\\\" + gManager.Connect(); // this is where it crashes } catch (UnauthorizedAccessException) { gStatus = eComputerCheckStatus.UnauthorizedAccessException; gHasError = true; return; } catch (Exception Ex) { if (Ex.Message.Contains("The RPC server is unavailable")) { gStatus = eComputerCheckStatus.FailedToContactWMIService; } else gStatus = eComputerCheckStatus.UnkownFailed; gHasError = true; return; } gStatus = eComputerCheckStatus.Connected; try { GetRunningProccessInfo(); GetCPUInformation(); GetHardDriveInfo(); GetOSInfo(); GetMemoryInfo(); GetMonitorInfo(); GetComputerSystem(); } catch { gStatus = eComputerCheckStatus.UnkownFailed; gHasError = true; } } 
+4
source share
3 answers

A good point to start is to check if this code throws an exception.

  ManagementScope scope = new ManagementScope(@"\\localhost\root\cimv2"); scope.Connect(); 

Update
You do not have a Monitor.Enter / Exit block when initializing iCI (and if an existing problem is eliminated).
to check if this is a problem, you can make a simple log.

  gManager = new ManagementScope(ConnectStr, oConn); Debug.WriteLine("connect to "+ComputerName+" by "+UserName+"/"+Password); gManager.Connect(); // this is where it crashes Debug.WriteLine("success on "+ComputerName+" by "+UserName+"/"+Password); 

If my suggestion is correct, you will receive in the log, something like this:
"connect to computer1"
"connect to computer2"
and an exceptional exception.

+1
source

This is pure speculation, but if this is what you are doing wrong, then it should be in the parameters that are passed to the gManger initialization.

The following, if you want to publish something, is an instance of the oConn object. There may be some incorrect parameters that cause problems later.

I did not use this class in my coding, therefore, despite the parameters, this is all ... with one caveat:

The MSDN article on ManagementScope had an interesting tid-bit about this library that cannot be used by partially trusted code.

For more information, see the .Net Framework Security section of this page .

Otherwise, good luck.

EDIT: Another thought. Since this happens with multiple threads, some problems may arise if a line tries to read / write from different threads. It might be helpful to see a larger context from your code.

I'm sorry that I won’t help here anymore, but maybe my thoughts will help you (or anyone else) solve this problem.

+1
source

I doubt that you will get a smoking gun answer here, as you have not posted enough code. Most StackOverflow errors come from a loop error.

I ran the code you submitted without any problems. Here is how I will debug:

Make sure you are not using any objects in streams! Close all connections and do not reuse them.

  • Run the code that you placed outside the thread loop.
  • Run the code in a loop without streaming.
  • Use thread pool instead of Threadstart. A good TP is SmartThreadPool , which has advantages over the built-in.

Try the above and if you still have a problem, write more code.

+1
source

All Articles