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);
}
}
}
运行结果:

本文展示了一个使用 C# 编写的简单事件处理示例,通过定义委托和事件,实现了一个进度更新的功能,并展示了如何订阅事件及触发事件来更新进度。
910

被折叠的 条评论
为什么被折叠?



