.NET Runtime error - unhandled exception - when using two threads from one thread

0

I get an error in the EventViewer when I try to add a second thread(pp). It works fine when I just run one thread(dev).

   protected override void OnStart(string[] args)
        {
            try
            {

            dev = new Thread(() => startRID("Dev"));
            dev.IsBackground = true;
            dev.Start();

            pp = new Thread(() => startRID("PreProd"));
            pp.IsBackground = true;
            pp.Start();
        }
        catch (Exception ex)
        {
            //Log the exception
            lock (eventLog)
            {
                eventLog.WriteEntry("Exception at OnStart: " + ex.Message, EventLogEntryType.Error, (int)EventId.InvalidApplicationSetting);
            }
        }
    }

This is the error message in the event viewer:

Application: Service.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ArgumentException
   at System.Diagnostics.EventLogInternal.WriteEntry(System.String, System.Diagnostics.EventLogEntryType, Int32, Int16, Byte[])
   at System.Diagnostics.EventLog.WriteEntry(System.String, System.Diagnostics.EventLogEntryType, Int32)
   at Service.Service.startRID(System.String)
   at Service.Service.<OnStart>b__4_1()
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.ThreadHelper.ThreadStart()

I also get an Application Error right after:

Faulting application name: Service.exe, version: 1.0.0.0, time stamp: 0x5a736c00
Faulting module name: KERNELBASE.dll, version: 6.1.7601.23915, time stamp: 0x59b94f2a
Exception code: 0xe0434352
Fault offset: 0x000000000001a06d
Faulting process id: 0x2d0c0
Faulting application start time: 0x01d39b94c5b56337
Faulting application path: D:\SomeFolder\Debug\Service.exe
Faulting module path: C:\windows\system32\KERNELBASE.dll
Report Id: 09dabb6d-0788-11e8-8a16-0025b50b0a5d

here is what startRID looks like. I added a bunch of catch and try to log the error to event viewer but it doesn't log it.

private void startRID(string farmName)
        {
            try
            {
                while (true)
                {
                    if (String.IsNullOrEmpty(ConfigurationManager.ConnectionStrings[farmName].ConnectionString))
                    {
                        eventLog.WriteEntry(string.Format("Exception in startRID method - no connectionString string value for " + farmName), EventLogEntryType.Error,
                        (int)EventId.GeneralException);
                        return;
                    }
                    RedisInfoDaemon rid = new RedisInfoDaemon(farmName);
                    try
                    {
                        rid.Start();
                    }
                    catch (Exception ex)
                    {
                        eventLog.WriteEntry(string.Format("Exception in startRID method - ex " + ex.Message), EventLogEntryType.Error,
                        (int)EventId.GeneralException);

                        try
                        {
                            rid.Reset(false);
                            rid.Start();
                        }
                        catch (Exception err)
                        {
                            eventLog.WriteEntry(string.Format("Exception in startRID method inside- err " + ex.Message), EventLogEntryType.Error,
                            (int)EventId.GeneralException);

                            Console.Write(err.ToString());
                            rid.Reset(true);
                            Thread.Sleep(TimeSpan.FromMinutes(5));
                            rid.Start();
                        }
                    }
                }
            }
            catch (Exception erer)
            {
                eventLog.WriteEntry(string.Format("Exception in startRID method - main - err " + erer.Message), EventLogEntryType.Error,
                (int)EventId.GeneralException);
            }
        }
c#
.net
multithreading
asked on Stack Overflow Feb 1, 2018 by TDeoodfig • edited Feb 1, 2018 by TDeoodfig

2 Answers

2

We can't see what startRID does, so we can't tell you what is exploding, but: what we can tell you is to not let exceptions hit the top of the call-stack, because that kills your app. So: maybe add a method like:

static void SafelyDoSomething(string key) {
    try { startRID(key); }
    catch(Exception ex) {
         // lots of logging here
         Console.Error.WriteLine(ex); // add more than this!
    }
}

and use new Thread(() => SafelyDoSomething("Dev")) / new Thread(() => SafelyDoSomething("PreProd")); etc. That should at least give you a clue what went wrong. But ultimately: it is inside the code we can't see. Emphasis: this is not a fix - it is a diagnostic tool.

answered on Stack Overflow Feb 1, 2018 by Marc Gravell
1

EventLog is not guaranteed to be thread-safe. You will have to rework your logic.

answered on Stack Overflow Feb 1, 2018 by Daniel A. White • edited Feb 1, 2018 by Daniel A. White

User contributions licensed under CC BY-SA 3.0