简体   繁体   中英

C# Multithreading Console runtime error

I have a Console app that uses a parallel.foreach producing a number of working threads that performs some tasks such as

  1. reading OS data through SNMP from a number of servers in our intranet and
  2. writes these values to a SQL server DB.

When I ran the code within Visual Studio 2010 in either debug- or release mode the program executes without exceptions. When I deploy the program and run it outside VS I get an exception (.NET Runtime Exceptiopn).

Stack Trace:

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[])

The AggregateException details are:

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

This is an unhandled aggregate exception . You will need to catch this type of error, loop through its exceptions and log each one to figure out what is going on.

You are probably not breaking on these types of exceptions in your debug routine.

Turn on Break on All Exceptions (Debug, Exceptions) or ( CTRL + ALT + E ) and rerun the program.
This will show you the original exception when it was thrown in the first place.

Catch code:

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

The attached exception is too abstract. The best approach would be handle the TPL exception and log. Things shall be much clearer when you do so. Then you can updated the post with proper exception.

Eg:

      try
        {
            YourSNMPTrapSenderMethod();
        }
       catch (AggregateException ae)
        {
            // This is where you can choose which exceptions to handle. 
            foreach (var ex in ae.InnerExceptions)
            {
               // log your exception. may be in temp directory
            }
        }

Or

        try
        {
            YourSNMPTrapSenderMethod();
        }
        catch (AggregateException ex)
        {
            ex.Handle((x) =>
            {
                Log.Write(x);
                return true;
            });
        }


   static void YourSNMPTrapSenderMethod()
    {
        var exceptions = new ConcurrentQueue<Exception>();
        Parallel.ForEach(data, d =>
        {
            try
            {
              //do your operations
            }

            catch (Exception e) { exceptions.Enqueue(e); }
        });


        if (exceptions.Count > 0) throw new AggregateException(exceptions);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM