委托和类,值一样是C#的数据类型...
使用委托要和使用其它类型一样定义现定义类型本身
就向定义类要用class关键字一样,委托..使用delegate关键字.
委托的用处就是可以把方法当成变量使用..
这段代码多处使用了委托...LZ请看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Build.Tasks.Deployment.ManifestUtilities;
using System.Threading;namespace WindowsApplication1
{
    public partial class Form1 : Form
    {        Thread thread1;
        Thread thread2;
        Thread thread3;
        Thread thread4;        delegate void VOID();
        
        Form2 form2 = new Form2();        bool end1 = false;
        bool end2 = false;
        bool end3 = false;
        bool end4 = false;        public Form1()
        {
            InitializeComponent();
            thread1 = new Thread(new ThreadStart(ThreadRun4));
            thread2 = new Thread(new ThreadStart(ThreadRun1));
            thread3 = new Thread(new ThreadStart(ThreadRun2));
            thread4 = new Thread(new ThreadStart(ThreadRun3));        }        void ThreadEnd()
        {
            if (end1 && end2 && end3 && end4)
            {
                
                form2.Close();
            }
        }        void ShowForm2()
        {
            if (form2.Visible != true)
            {
                form2.Show(this);
            }
        }
        
        float plan1 = 0;
        float plan2= 0;
        float plan3 = 0;
        float plan4 = 0;
        void SetPlan()
        {
            float temp = (plan1 + plan2 + plan3 + plan4) * 10000 * 0.25f;            form2.progressBar1.Value = (int)Math.Round(temp);            
            form2.progressBar2.Value = (int)Math.Round(plan1 * 10000);
            form2.progressBar3.Value = (int)Math.Round(plan2 * 10000);
            form2.progressBar4.Value = (int)Math.Round(plan3 * 10000);
            form2.progressBar5.Value = (int)Math.Round(plan4 * 10000);
        }                private void button1_Click(object sender, EventArgs e)
        {
            thread1.Start();
            thread2.Start();
            thread3.Start();
            thread4.Start();
        }        Random random = new Random();        void ThreadRun4()
        {
            int maxCount;
            int sleep;
            lock (random)
            {
                maxCount = random.Next(10, 100);
                sleep = random.Next(100, 500);
            }
            this.Invoke(new VOID(ShowForm2));            
            for (int a = 0; a <= maxCount;a++ )
            {
                Thread.Sleep(sleep);
                plan4 = (float)a / maxCount;
                this.Invoke(new VOID(SetPlan));
            }
            end4 = true;
            this.Invoke(new VOID(ThreadEnd));
        }        void ThreadRun1()
        {            int maxCount;
            int sleep;
            lock (random)
            {
                maxCount = random.Next(10, 100);
                sleep = random.Next(100, 500);
            }
            this.Invoke(new VOID(ShowForm2));            
            for (int a = 0; a <= maxCount; a++)
            {
                Thread.Sleep(sleep);
                plan1 = (float)a / maxCount;
                this.Invoke(new VOID(SetPlan));
            }
            end1 = true;
            this.Invoke(new VOID(ThreadEnd));
        }
        void ThreadRun2()
        {            int maxCount;
            int sleep;
            lock (random)
            {
                maxCount = random.Next(10, 100);
                sleep = random.Next(100, 500);
            }
            this.Invoke(new VOID(ShowForm2));            
            
            for (int a = 0; a <= maxCount; a++)
            {
                Thread.Sleep(sleep);
                plan2 = (float)a / maxCount;
                this.Invoke(new VOID(SetPlan));
            }
            end2 = true;
            this.Invoke(new VOID(ThreadEnd));
        }
        void ThreadRun3()
        {
            int maxCount;
            int sleep;
            lock (random)
            {
                maxCount = random.Next(10, 100);
                sleep = random.Next(100, 500);
            }
            this.Invoke(new VOID(ShowForm2));            
            
            for (int a = 0; a <= maxCount; a++)
            {
                Thread.Sleep(sleep);
                plan3 = (float)a / maxCount;
                this.Invoke(new VOID(SetPlan));
            }
            end3 = true;
            this.Invoke(new VOID(ThreadEnd));
        }       
        
       
    }
}

解决方案 »

  1.   

    委托就是一种机制,这种机制使得从一个类中调用其它类中方法变得简单而清晰。降低了类与类之间的耦合度和程序设计的复杂程度
    委托可以说是“.NET版的函数指针”,实际上,委托较之函数指针有了很多改进,其中最实用也是最酷的一点就是:委托是“多播”的,而函数指针是“单播”的。换句话说:委托是“一对多的”,即一个委托上可以挂接好几个与之签名相同的函数;而函数指针只能“一对一”,即一个函数指针只能指向一个函数
      

  2.   


    int i = 10;
    string message = "hello,world!";
      

  3.   

    不用委托一样可以实现相应功能,不过比较麻烦
    class A {
      public void test(string text) {
        Console.WriteLine(text);
      }
    }void ff(A a) {
      a.test("中国人");
    }
      

  4.   

    大家对这委托这么热情,我也说两句
    用委托并不很难,在什么情况下用,怎样用才是最难的,
    委托用的地方很多,我所知的
    1事件上,这个不多说
    2多线程上,在2003上thread是不安全的线程,在2005引入了委托机制弥补线程不安全的缺点
    使windows的消息得以同步
    3自动业务流程上,运用多播委托可以实现,