下面这段代码,
        public static void test()
        {
            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(ExceptionMethod));
            try
            {
                thread.Start();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }        public static void ExceptionMethod()
        {
               throw new Exception("Error.");
        }会出错,原因是try捕捉不到thread 里的例外,有什么办法可以让try捕捉得到呢?除了在ExceptionMethod方法里面加try外。

解决方案 »

  1.   

    如果用delegate的BeginInvoke完成异步,那么.Net会把Exception传递到调用线程:delegate void Func();        public static void test()
            {
                try
                {
                    Func func = new Func(ExceptionMethod);
                    IAsyncResult res = func.BeginInvoke(null, null);
                    func.EndInvoke(res);
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
      

  2.   

    Exception会在调用EndInvoke的是后throw出来。
      

  3.   

    謝謝 qqchen79(知秋一叶)這個方法確實是可以,但是有沒有使用Thread的方法呢,因為我需要使用Thread後,保存這個Thread,然后在必要的時候去Abort它。
      

  4.   

    你在THREAD里面TRY然后SEND MESSAGE算了...
      

  5.   

    用Thread没有简单的办法。
    而且,一般不建议使用Thread.Abort,用其他想成同步方法会更可靠一些。