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));
} }
}
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));
} }
}
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);
} }
}
}