gpt4 book ai didi

C# 多线程控制台运行时错误

转载 作者:太空宇宙 更新时间:2023-11-03 20:09:56 30 4
gpt4 key购买 nike

我有一个控制台应用程序,它使用 parallel.foreach 生成许多工作线程来执行某些任务,例如

  1. 通过 SNMP 从我们内联网中的多个服务器读取操作系统数据,以及
  2. 将这些值写入 SQL 服务器数据库。

当我在 Visual Studio 2010 中以调试或 Release模式运行代码时,程序无一异常(exception)地执行。当我部署程序并在 VS 外部运行它时,出现异常(.NET 运行时异常)。

堆栈跟踪:

Application: ServerMonitorSNMP.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.AggregateException Stack: at System.Threading.Tasks.Parallel.ForWorker[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](Int32, Int32, System.Threading.Tasks.ParallelOptions, System.Action`1,

...

at ServerMonitoringSNMP.Program.Main(System.String[])

AggregateException 的详细信息是:

System.UnhandledExceptionEventArgs System.AggregateException: One or more errors occurred. ---> System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

...

(Inner Exception #0) System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

...

(Inner Exception #1) System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

...

(Inner Exception #2) System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

...

System.AggregateException: One or more errors occurred. ---> System.NullReferenceException: Object reference not set to an instance of an object. at ServerMonitoringSNMP.Program.GetStorageValue(Dictionary`2 StorageQuery, Int32 diskOidIndex) in \Program.cs:line 896

最佳答案

这是一个未处理的 aggregate exception .您将需要捕获此类错误,遍历其异常并记录每个异常以弄清楚发生了什么。

您可能不会在调试例程中中断这些类型的异常。

打开 Break on All Exceptions (Debug, Exceptions) 或 (CTRL + ALT + E) 并重新运行程序。
这将向您显示最初抛出异常时的原始异常。

捕获代码:

catch (AggregateException e)
{
foreach (var ex in e.InnerExceptions)
{
//log here
}
}

关于C# 多线程控制台运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20663146/

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