gpt4 book ai didi

c# - Stackoverflow 调用 ManagementScope.Connect();

转载 作者:行者123 更新时间:2023-11-30 12:18:42 25 4
gpt4 key购买 nike

我得到的错误:

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

我的调用栈:

[Managed to Native 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 = "") Line 228 + 0xd bytes C#
Computer_Managerment.exe!Computer_Managerment.ScanAllComputers.Workerthread() Line 95 + 0x1e bytes C#
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 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

获取stackoverflow的代码:

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

代码基本可以在这个fasion中运行
1) 我有一个 AD 中所有计算机的列表(超过 1k)
2) 我有 10 个线程在一个 while 循环中旋转,从队列列表中获取一个计算机。
3)当他们有一个计算机名时,他们创建了一个执行 gManager.Connect(); 的 ComputerInformation 类的实例;再次崩溃。

我的理解是这个崩溃/stackoverflow 发生在 native 代码中,但我认为我做错了什么。如果需要更多代码,我很乐意发布。

Pr request more code: 这是 worker 居住的代码(一般10个 worker 左右)

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();
}
}

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;
}
}

最佳答案

好的开始点是检查这段代码是否抛出异常。

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

更新
您在 iCI 初始化时没有 Monitor.Enter/Exit block (以及存在问题异常的地方)。
要检查这是否是您可以制作简单日志的问题。

  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);

如果我的建议是正确的,您将在日志中收到如下内容:
“连接到计算机 1”
“连接到计算机 2”
并立即异常(exception)。

关于c# - Stackoverflow 调用 ManagementScope.Connect();,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1519524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com