c# 回调委托

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Wind2
{
    public partial class Form1 : Form
    {
        public delegate int del(int num1, int num2);
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            del a = test;
            int num1 = int.Parse(this.textBox1.Text);
            int num2 = int.Parse(this.textBox2.Text);
            a.BeginInvoke(num1, num2,method, a);
        }

        public int test(int num1, int num2)
        {
            int sum = 0;
            for (int i = num1; i <= num2; i++)
            {
                sum += num1;
                num1++;
            }
            return sum;
        }

        public void method(IAsyncResult res) 
        {
            if (res != null)
            {
               this.label1.Text=(res.AsyncState as del).EndInvoke(res).ToString();
            }
         
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
        }
    }
}

  

posted @ 2016-06-16 09:50  尘梦  阅读(168)  评论(0编辑  收藏  举报