Divider.csusing System;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Threading;
using System.Runtime.Remoting.Messaging;public delegate int Comuter(int a, int b);public delegate void DivCompleteHandler(object sender, DivCompleteEventArgs e);
public class Divider
{
    public event DivCompleteHandler OnDivComplete;
    public Divider()
    {    }    public int Div(int a, int b)
    {
        Thread.Sleep(5000);        try
        {
            return a / b;
        }
        catch (DivideByZeroException e)
        {
            throw e;
        }
    }    public void DivAsyn(int a, int b)
    {
        Comuter computer = new Comuter(this.Div);
        computer.BeginInvoke(a, b, new AsyncCallback(Response), null);
    }    void Response(IAsyncResult result)
    {
        try
        {
            Comuter computer = ((AsyncResult)result).AsyncDelegate as Comuter;            if (null != computer)
            {
                int consult = computer.EndInvoke(result);
                OnDivComplete(this, new DivCompleteEventArgs(consult, null, false, null));
            }
        }
        catch (DivideByZeroException e)
        {
            OnDivComplete(this, new DivCompleteEventArgs(0, e, true, null));
        }
        catch (Exception e)
        {
            OnDivComplete(this, new DivCompleteEventArgs(0, e, true, null));
        }    }
}

解决方案 »

  1.   

    MainFrame.csusing System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Reflection;
    namespace DividerForm
    {
        
        internal delegate void SetResult(string content);
        
        public partial class MainFrame : Form
        {
            public MainFrame()
            {
                InitializeComponent();
            }        private void btnDivide_Click(object sender, EventArgs e)
            {
                int num1 = Convert.ToInt32(this.tbNum1.Text);
                int num2 = Convert.ToInt32(this.tbNum2.Text);            Divider divider = new Divider();
                divider.OnDivComplete+=new DivCompleteHandler(GetDivResultComplete);            divider.DivAsyn(num1, num2);        }        private void MainFrame_Load(object sender, EventArgs e)
            {        }        public void SetResultText(string content)
            {
                this.result.Text=content;
            }        private void GetDivResultComplete(object sender, DivCompleteEventArgs e)
            {            if (!e.Cancelled)
                {
                    string content = e.Result.ToString();
                    SetResult sr = new SetResult(SetResultText);
                    this.result.Invoke(sr, new string[] { content });
                }
                else
                {
                    MessageBox.Show(e.Error.Message);
                }        }
        }
    }
      

  2.   

    PS:没有发生异常的适合,我的程序是可以工作的,当除数为0的时候,异常抓不到,针对BeginInvoke,该怎么抓异常呢
      

  3.   

    一般来说跨线程设计时不要通过Exception机制在线程间传递信息。
      

  4.   

    如果lz确实想要这个feature,建议lz看看remoting中对异常的处理。