event 自定义事件一例

来源:互联网 发布:知乎 找工作 编辑:程序博客网 时间:2024/06/05 21:18
 
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace ConsoleTest{    public delegate void EventHandler(string sInfo);    class Class1    {        public event EventHandler EventHandleTest;        public void Start()        {            if (EventHandleTest != null)            {                Thread.Sleep(1000);                EventHandleTest("完成进度20%");                Thread.Sleep(1000);                EventHandleTest("完成进度40%");                Thread.Sleep(1000);                EventHandleTest("完成进度60%");                Thread.Sleep(1000);                EventHandleTest("完成进度80%");                Thread.Sleep(1000);                EventHandleTest("完成进度100%");            }        }    }    class customEvent    {        static void Main()        {            Class1 c = new Class1();            c.EventHandleTest +=new EventHandler(c_EventHandleTest);            c.Start();            Console.ReadLine();        }        public static void c_EventHandleTest(string sInfo)        {            Console.WriteLine(sInfo);        }    }}


运行结果:

原创粉丝点击