delegates gives functionality of creating additional threads…
examples
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; using System.Runtime.Remoting.Messaging; namespace SyncDelegateReview { public delegate int BinaryOp(int x, int y); class Program { private static bool isDone = false; static void Main(string[] args) { //SyncCall(); //ASyncCall(); //ASyncCall2(); // works like annoying manager, always asks if additional thread isCompleted //ASyncCall3(); // works like annoying manager, always asks if additional thread isCompleted AsyncCallBackExample(); } static int Add(int x, int y) { Console.WriteLine("Add() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId ); Thread.Sleep(5000); return x+y; } static void SyncCall() // << blocks main thread { Console.WriteLine("*** Sync Delegate Review ***"); Console.WriteLine("Main() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId); BinaryOp b = new BinaryOp(Add); int answer = b(10, 10); // in main thread, sync mode... Console.WriteLine("Doing more work in Main()"); Console.WriteLine("10 + 10 is {0}", answer); Console.ReadLine(); } static void ASyncCall() // << blocks main thread { Console.WriteLine("*** ASync Delegate Review ***"); Console.WriteLine("Main() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId); BinaryOp b = new BinaryOp(Add); //int answer = b(10, 10); // in main thread, sync mode... IAsyncResult itf = b.BeginInvoke(10, 10, null, null); Console.WriteLine("Doing more work in Main()!"); // getting result int answer = b.EndInvoke(itf); Console.WriteLine("10 + 10 is {0}", answer); Console.ReadLine(); } static void ASyncCall2() // << allows work in main thread { Console.WriteLine("*** ASync Delegate Review ***"); Console.WriteLine("Main() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId); BinaryOp b = new BinaryOp(Add); //int answer = b(10, 10); // in main thread, sync mode... IAsyncResult itf = b.BeginInvoke(10, 10, null, null); while (!itf.IsCompleted) // always ask additional thread IsCompleted and works during this { Console.WriteLine("DoingMoreWorkInMain"); Thread.Sleep(1000); } Console.WriteLine("Doing more work in Main()!"); int answer = b.EndInvoke(itf); Console.WriteLine("10 + 10 is {0}", answer); Console.ReadLine(); } static void ASyncCall3() // << allows work in main thread with max time limit { Console.WriteLine("*** ASync Delegate Review ***"); Console.WriteLine("Main() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId); BinaryOp b = new BinaryOp(Add); //int answer = b(10, 10); // in main thread, sync mode... IAsyncResult itf = b.BeginInvoke(10, 10, null, null); while (!itf.AsyncWaitHandle.WaitOne(1000,true)) // max time to wait //always ask additional thread IsCompleted and works during this { Console.WriteLine("DoingMoreWorkInMain"); Thread.Sleep(1000); } Console.WriteLine("Doing more work in Main()!"); int answer = b.EndInvoke(itf); Console.WriteLine("10 + 10 is {0}", answer); Console.ReadLine(); } static void AsyncCallBackExample() { Console.WriteLine("*** ASyncCallBackExample ***"); Console.WriteLine("Main() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId); BinaryOp b = new BinaryOp(Add); //int answer = b(10, 10); // in main thread, sync mode... IAsyncResult itf = b.BeginInvoke( 10, // delegate param 10, // delegate param new AsyncCallback(AddComplete), // CallBack method "Main() string object added to call back method"); // some object for call back method while (!isDone) // { Console.WriteLine("DoingMoreWorkInMain"); Thread.Sleep(1000); } Console.ReadLine(); } static void AddComplete(IAsyncResult itf) { Console.WriteLine("AddComplete() invoked on thread {0} ", Thread.CurrentThread.ManagedThreadId); AsyncResult ar = (AsyncResult)itf; BinaryOp b = (BinaryOp)ar.AsyncDelegate; string msg = (string)itf.AsyncState; // could be any object not only string... Console.WriteLine("10+10 is {0}",b.EndInvoke(itf)); Console.WriteLine("message from Main " + msg); isDone = true; } } } |