ThreadA产生ThreadB。通知另一个异常的线程
ThreadB引发异常。
ThreadA如何知道这个异常?
using System;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ThreadStart threadDelegate1 = new ThreadStart(MyClass1.DoThis);
Thread ThreadA = new Thread(threadDelegate1);
ThreadA.Start();
Thread.Sleep(100000); // this thread is doing something else here, sleep simulates it
}
}
class MyClass1
{
public static void DoThis()
{
try
{
ThreadStart threadDelegate1 = new ThreadStart(MyClass2.DoThat);
Thread ThreadB = new Thread(threadDelegate1);
ThreadB.Start();
Thread.Sleep(100000); // this thread is doing something else here, sleep simulates it
}
catch (Exception e)
{
// I want to know if something went wrong with MyClass2.DoThat
}
}
}
class MyClass2
{
public static void DoThat()
{
throw new Exception("From DoThat");
}
}
}
似乎是[这酮]重复(http://stackoverflow.com/questions/2810468/c-exception-handling-on-background-threads-使用线程池) – 2011-03-14 19:59:40
不寻找python的例子:) – 2011-03-14 20:02:09
你在两个编辑之间发现了这个评论:) – 2011-03-14 20:06:00