Exceptions in multithreaded application.

前端 未结 10 1513
春和景丽
春和景丽 2021-02-15 00:02

I have heard from a very discerning person that an exception being thrown (and not caught) in a thread is being propagated to the parent thread. Is that true? I have tried some

相关标签:
10条回答
  • 2021-02-15 00:07

    A decent resource on Threading in C# that has a section for exception handling: Threading in C#

    0 讨论(0)
  • 2021-02-15 00:07

    In short, no it doesn't. You have a couple of obvious options:#

    1. log it out in the method your new thread starts (top of the stack for the created thread) in.
    2. Use a construct like Asynchronous delegate, which will will return an exception when you call end invoke, which you can then catch in the normal way.
    0 讨论(0)
  • 2021-02-15 00:12

    This is a great article about Threading in C# and how to handle Exceptions

    0 讨论(0)
  • 2021-02-15 00:16

    Exception propagate upwards in the call-stack.

    If you start a new thread from a certain method, it will propagate upwards until it gets to that method.

    If that method doesn't catch it, you'll get a runtime error that there's an uncaught exception.

    0 讨论(0)
  • 2021-02-15 00:16

    An exception can only be caught on the thread from which is came. So throwing an exception on another thread won't cause another thread to catch it.

    There is no concept of a "parent" thread.

    0 讨论(0)
  • 2021-02-15 00:16

    There is a very simple solution for the special case when the main thread waits for the completion of the other thread (see https://msdn.microsoft.com/en-us/library/58195swd(v=vs.110).aspx#Examples). It requires only a class exception variable.

    The modified code sample completed by an exception handler may look like this.

    using System;
    using System.Threading;
    
    class WaitOne
    {
        static AutoResetEvent autoEvent = new AutoResetEvent(false);
        static Exception SavedException = null;
    
        static void Main()
        {
            Console.WriteLine("Main starting.");
    
            ThreadPool.QueueUserWorkItem(
                new WaitCallback(WorkMethod), autoEvent);
    
            // Wait for work method to signal.
            autoEvent.WaitOne();
            if (SavedException != null)
            {
               // handle this exception as you want
            }
    
            Console.WriteLine("Work method signaled.\nMain ending.");
        }
    
        static void WorkMethod(object stateInfo) 
        {
            Console.WriteLine("Work starting.");
    
            // Simulate time spent working.
            try
            {
              Thread.Sleep(new Random().Next(100, 2000));
              throw new Exception("Test exception");
            }
            catch (Exception ex)
            {
                SavedException = ex;
            }            
    
            // Signal that work is finished.
            Console.WriteLine("Work ending.");
            ((AutoResetEvent)stateInfo).Set();
        }
    }
    
    0 讨论(0)
提交回复
热议问题