class ClassA { public event Action eventTest; public void fun1(int a,int b) { Console.WriteLine("fun1 start"); Console.WriteLine("a+b=" + (a + b).ToString()); Console.WriteLine("fun1 sleep start"); eventTest?.Invoke(); // Thread.Sleep(1000); Console.WriteLine("fun1 sleep stop"); // return true; } } class ClassB { public delegate void dlg(int num1, int num2); public void fun(int a, int b,dlg fa) { Console.WriteLine("classB start"); fa(a, b); Console.WriteLine("classB stop"); } } //**main form 按钮 private void button18_Click(object sender, EventArgs e) { ClassA A = new ClassA(); ClassB B = new ClassB(); A.eventTest += () => { Console.WriteLine("eventTest start"); Console.WriteLine("eventTest sleep start"); Thread.Sleep(1000); Console.WriteLine("eventTest sleep end"); }; Console.WriteLine("mainform start"); B.fun(2, 3, A.fun1); Console.WriteLine("mainform stop"); }
此例的回调和事件的触发顺序相当于单线程下的运行顺序
输出结果: mainform start classB start fun1 start a+b=5 fun1 sleep start eventTest start eventTest sleep start eventTest sleep end fun1 sleep stop classB stop mainform stop