public partial class Form1 : Form
{
public delegate int GetCalculateDelegate(int beginNum, int endNum);
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 执行一个计算方法,指定时间间隔后+1,模拟耗时操作,
/// 并将计算完成的数字显示出来,如果不使用异步,则UI线程会被阻塞,等待计算方法执行完成
/// 1.定义一个类,里面包含一个计数方法
/// 2.定义一个委托,使用异步执行该计数方法,并回调获取该计数结果
/// 3.显示回调结果
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
Calculate model = new Calculate();
//定义委托方法
GetCalculateDelegate gcdel = model.GetCalculateResult;
//开始计数
this.textBox1.Text = "开始计数结果:";
//开始异步执行
gcdel.BeginInvoke(1, 100, CalculateCompleteBack, gcdel);
}
/// <summary>
/// 更新ttextbox显示
/// </summary>
public void CalculateComplete(int result)
{
this.textBox1.Text =string.Format("计数结果为:{0}", result.ToString());
}
/// <summary>
/// 回调方法
/// </summary>
/// <param name="result"></param>
public void CalculateCompleteBack(IAsyncResult ar) {
if (ar == null) return;
//转化为委托 GetCalculateDelegate
GetCalculateDelegate gcdel = ar.AsyncState as GetCalculateDelegate;
if (gcdel == null) throw new Exception("转化类型失败!");
//获取返回结果
int result = gcdel.EndInvoke(ar);
this.Invoke(new Action(() => CalculateComplete(result)));
}
}
/// <summary>
/// 计算类
/// </summary>
public class Calculate {
/// <summary>
/// 将制定的值开始相加知道结束
/// </summary>
/// <param name="begin"></param>
/// <param name="end"></param>
/// <returns></returns>
public int GetCalculateResult(int begin,int end) {
int result = 0;
for (int i = begin; i < end; i++)
{
//计算
result += i;
//间隔
System.Threading.Thread.Sleep(100);
}
return result;
}
}
{
public delegate int GetCalculateDelegate(int beginNum, int endNum);
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 执行一个计算方法,指定时间间隔后+1,模拟耗时操作,
/// 并将计算完成的数字显示出来,如果不使用异步,则UI线程会被阻塞,等待计算方法执行完成
/// 1.定义一个类,里面包含一个计数方法
/// 2.定义一个委托,使用异步执行该计数方法,并回调获取该计数结果
/// 3.显示回调结果
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
Calculate model = new Calculate();
//定义委托方法
GetCalculateDelegate gcdel = model.GetCalculateResult;
//开始计数
this.textBox1.Text = "开始计数结果:";
//开始异步执行
gcdel.BeginInvoke(1, 100, CalculateCompleteBack, gcdel);
}
/// <summary>
/// 更新ttextbox显示
/// </summary>
public void CalculateComplete(int result)
{
this.textBox1.Text =string.Format("计数结果为:{0}", result.ToString());
}
/// <summary>
/// 回调方法
/// </summary>
/// <param name="result"></param>
public void CalculateCompleteBack(IAsyncResult ar) {
if (ar == null) return;
//转化为委托 GetCalculateDelegate
GetCalculateDelegate gcdel = ar.AsyncState as GetCalculateDelegate;
if (gcdel == null) throw new Exception("转化类型失败!");
//获取返回结果
int result = gcdel.EndInvoke(ar);
this.Invoke(new Action(() => CalculateComplete(result)));
}
}
/// <summary>
/// 计算类
/// </summary>
public class Calculate {
/// <summary>
/// 将制定的值开始相加知道结束
/// </summary>
/// <param name="begin"></param>
/// <param name="end"></param>
/// <returns></returns>
public int GetCalculateResult(int begin,int end) {
int result = 0;
for (int i = begin; i < end; i++)
{
//计算
result += i;
//间隔
System.Threading.Thread.Sleep(100);
}
return result;
}
}