1.以下这段代码是在主窗体中的。        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Class1 c = new Class1();
                new Process(c.TestMethod).BeginInvoke(null,null);
            }
            catch(Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }
2.这个是在一个新建的类文件中。    public delegate void Process();    public class Class1
    {
        public void TestMethod()
        {
            long i = 0;
            for (; i < 90000000; i++) ; //拖时间
            i = 0;
            long j = 100 / i; //会出现异常除0
        }
    }
如果我用BeginInvoke的话,在TestMethod中出现的异常在主窗体线程中是捕获不到的,大家是怎么做的这部分?而且我发现如果直接运行exe的话,程序没有任何反应,但如果以调试模式运行的话就会出现异常。但无论如何也不能捕获和处理它。

解决方案 »

  1.   


        public delegate void Process();    public class Class1
        {
            public void TestMethod()
            {
                long i = 0;
                for (; i < 90000000; i++) ; //拖时间
                  try
                {
                  i = 0;
                long j = 100 / i; //会出现异常除0
                }
                catch(Exception ex)
                {
                   // Invoke pass to main thread if required here.
                }
            }
        }
      

  2.   

    IAsyncResult result  = new Process(c.TestMethod).BeginInvoke(null,null);
    Thread.Sleep(0);
    // Poll while simulating work.
                while(result.IsCompleted == false) {
                    Thread.Sleep(10);
                }            // Call EndInvoke to retrieve the results.
                string returnValue = caller.EndInvoke(out threadId, result);
      

  3.   

    因为异步调用,在另外线程出现错误时,捕获时机已经错过
       try
                {
                    Class1 c = new Class1();
                    new Process(c.TestMethod).BeginInvoke(null,null);//启动另外一个线程,此函数接着向下执行,不会等待那个线程出现错误
                }
                catch(Exception exp) 
                {
                    MessageBox.Show(exp.Message);
                }改为
    try{
                    class1 c = new class1();
                    Process ps = new Process(c.TestMethod);
                    IAsyncResult ir = ps.BeginInvoke(null, c);
                    ps.EndInvoke(ir);
        }
    catch(Exception ex )
    {
       MessageBox.Show(ex.ToString());
    }            
      

  4.   

    我最近也刚接触异步调用,在 MSDN 中看到的例子似乎都是结束时才处理可能发生过的异常。
      

  5.   

    每一个BeginInvoke都要有对应的EndInvoke,异常可以在EndInvoke时获得
      

  6.   

    好像没用呀,还是捕获不到。    public partial class Form1 : Form
        {
            Process process = null;
            public Form1()
            {
                InitializeComponent();
                Class1 c = new Class1();
                process = new Process(c.TestMethod);
            }        private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    process.BeginInvoke(null, null);
                }
                catch(Exception exp)
                {
                    MessageBox.Show(exp.Message);
                }
            }        private void EndProcess(IAsyncResult ar)
            {
                try
                {
                    process.EndInvoke(ar);
                    MessageBox.Show("Task complete!");
                }
                catch
                {
                    throw;
                }
                finally
                {
                    this.Invoke(new Process(this.Close));
                }
            }
        }
    调试时还会出错。
      

  7.   

        try
                {
    IAsyncResult   ir   =   process.BeginInvoke(null,null); 
    process.EndInvoke(ir);
                }
                catch(Exception exp)
                {
                    MessageBox.Show(exp.Message);
                }
      

  8.   

    刚才忘记把EndProcess加进去了,呵呵现在可以捕获到异常,但为什么在调试的时候捕获不到?
      

  9.   


                                    if (!this.cardTimeBll.Exists(para1.ToString(), Convert.ToDateTime(para4)))
                                    {
                                        if (temp.FCar == string.Empty)
                                        {
                                            temp.FCar = "##";
                                        }
                                        bool isInsert = this.cardTimeBll.Add(temp);
                                        if (!isInsert)
                                        {
                                            temp.FCarState = "##";
                                        } 
                                        IAsyncResult iResult = this.listView1.BeginInvoke(new UpdateListViewEventHandler(updateListView), new object[] { temp });
                                        this.listView1.EndInvoke(iResult);
                                    }