事件关联的委托是按顺序串行执行的。
View Code
using System; namespace EventDemo { class Program { static void Main(string[] args) { Job job = new Job(); job.DoJob(); } } class Job { public delegate void JobHandle(int max); public event JobHandle OnWork; public Job() { OnWork += new JobHandle(new Job1().DoJob); OnWork += new JobHandle(new Job2().DoJob); } public void DoJob() { OnWork(5); } } class Job1 { public void DoJob(int max) { for (int i = 0; i < max; i++) { Console.WriteLine("[{0}]{1}", this, i); System.Threading.Thread.Sleep(2000); } } } class Job2 { public void DoJob(int max) { for (int i = 0; i < max; i++) { Console.WriteLine("[{0}]{1}", this, i); System.Threading.Thread.Sleep(1000); } } } }
显示结果:
[EventDemo.Job1]0
[EventDemo.Job1]1
[EventDemo.Job1]2
[EventDemo.Job1]3
[EventDemo.Job1]4
[EventDemo.Job2]0
[EventDemo.Job2]1
[EventDemo.Job2]2
[EventDemo.Job2]3
[EventDemo.Job2]4
请按任意键继续. . .