Delegate execute code in additional thread in Sync or Async modes, main methods
Invoke() – sync mode
BeginInvoke() – async mode
EndInvoke() async mode
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SimpleDelegate { class Program { static void Main(string[] args) { SimpleMath sm = new SimpleMath(); BinaryOp b = new BinaryOp(sm.Add); Console.WriteLine("direct delegate call "+b(10, 10)); // or Console.WriteLine("invoke call "+b.Invoke(10, 10)); DisplayDelegateInfo(b); Console.ReadLine(); } static void DisplayDelegateInfo(Delegate delObj) { foreach (Delegate d in delObj.GetInvocationList()) { Console.WriteLine("Method Name: {0}",d.Method); // method name Console.WriteLine("Type Name: {0}", d.Target); // type name } } } public delegate int BinaryOp(int x, int y); public class SimpleMath { public int Add(int x, int y) { return x + y; } public int Subtract(int x, int y) { return x - y; } } } |