C# Event 多个委托

public static event EventHandler<TestEventArgs> test;
public static void Main()
{
test += TestMethod1;
test += TestMethod2;
test += TestMethod3;
test += TestMethod4;
test(null,new TestEventArgs());
Console.Read();
}

private static void TestMethod1(object sender, TestEventArgs e)
{
Thread.Sleep(1000);
Console.WriteLine("Method1");
e.Number++;
Console.WriteLine(e.Number);
}
private static void TestMethod2(object sender, TestEventArgs e)
{
Console.WriteLine("Method2");
e.Number++;
Console.WriteLine(e.Number);
}
private static void TestMethod3(object sender, TestEventArgs e)
{
Console.WriteLine("Method3");
e.Number++;
Console.WriteLine(e.Number);
}
private static void TestMethod4(object sender, TestEventArgs e)
{
Console.WriteLine("Method4");
e.Number++;
Console.WriteLine(e.Number);
}

public class TestEventArgs : EventArgs
{
public int Number
{
get;
set;
}
}

}

test

 

多个委托按照顺序执行,且EventArgs共用

Posted in C#

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据