static void Main() { Console.WriteLine("[{0}] Main called", Thread.CurrentThread.ManagedThreadId); BinaryOperation asyncAdd = Add; //Add method run by threadpool thread; //Third parameter here is the call back method asyncAdd.BeginInvoke(2, 2, null, null); Thread.Sleep((1000)); }
static void Main() { Console.WriteLine("[{0}] Main called", Thread.CurrentThread.ManagedThreadId); BinaryOperation asyncAdd = Add; //Add method run by threadpool thread; IAsyncResult asyncResult = asyncAdd.BeginInvoke(2, 2, new AsyncCallback(callBack), null); //Blocks the main thread till the add operation invoked by Begin Invoke finishes. int res = asyncAdd.EndInvoke(asyncResult); Console.WriteLine("BeginInvoke finished with result {0}", res); //Thread.Sleep((1000)); }
static void Main(string[] args) { cw("**** Synch Delegate Review ****"); cw("Main invoked on thread " + Thread.CurrentThread.ManagedThreadId); BinaryOperation add = Add; // begin async add.BeginInvoke(5, 5, AddComplete, "#Hello"); while (!isDone) { Thread.Sleep(1000); cw("Doing more work in Main"); } cr(); }
/* * * ::: General Notes on Multi Threading ::: * * Threads are core concurrent programming construct in .NET * * Use: System.Threading.Thread (Old), System.Threading (Current) * * - Contained in/within single process. * - Share access to any/all data within process/AppDomain * - Independently prioritized & scheduled for CPU time by OS * - Leveraging Threads (Manual/Explicit; Thread Pool; Async I/O) * * QueueUserWorkItem / Delegates :: * Dispatch a pooled thread to call a method that may take a long time to complete. * * Delegates: provide type-aware interface to thread pool * - Classes that represent a method call (signature and target instance) * - Compiled-generated BeginInvoke matches method signature * - CLR implements BeginInvoke at run-time to queue thread pool request * * * Async I/O is inverse use model. Enqueuing request to perform I/O not dispatch thread. * - Only when I/O completes do you dispatch thread. * */ //static volatile bool cancel = false; static void Main(string[] args) { Console.WriteLine("Multi Threading Demos\r\n"); Console.WriteLine("[{0}], Main called", Thread.CurrentThread.ManagedThreadId); Console.WriteLine("[{0}] Processor/core count = {1} \r\n[{0}] Is 64 Bit OS = {2}", Thread.CurrentThread.ManagedThreadId, Environment.ProcessorCount, Environment.Is64BitOperatingSystem); // ThreadPool.QueueUserWorkItem(DisplayMessage, "Hello, World!"); //Thread t = new Thread(SayHello); //t.Name = "Hello Thread"; //t.Priority = ThreadPriority.BelowNormal; //t.IsBackground = true; //t.Start(10); //for (int n = 0; n < 10; n++) //{ // ThreadPool.QueueUserWorkItem(SayHello, n); //} #if (false) Add(2, 2); #else BinaryOperation asyncAdd = Add; asyncAdd.BeginInvoke(2, 2, null, null); Thread.Sleep(1000); #endif Thread.Sleep(rng.Next(1000, 5000)); Console.WriteLine("[{0}] Main done", Thread.CurrentThread.ManagedThreadId); //Thread.Sleep(5000); // Keep the console open in debug mode. Console.WriteLine(System.Environment.NewLine); Console.WriteLine("Press any key to exit . . . "); Console.ReadKey(); //cancel = true; //t.Join(); //Console.WriteLine("Done"); }
static void Main(string[] args) { try { Console.WriteLine($"Main method started {Thread.CurrentThread.ManagedThreadId}"); BinaryOperation objBinary = AddAndMult; IAsyncResult cookie = objBinary.BeginInvoke(3, 4, null, null); Thread.Sleep(1000); Console.WriteLine($"Main method ended {Thread.CurrentThread.ManagedThreadId}"); int res = objBinary.EndInvoke(cookie); Console.WriteLine(res); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine("Exception Raised"); Console.ReadLine(); } }
static void Main(string[] args) { cw("**** Synch Delegate Review ****"); cw("Main invoked on thread " + Thread.CurrentThread.ManagedThreadId); BinaryOperation add = Add; // begin async var asyncResult = add.BeginInvoke(5, 5, null, null); while (!asyncResult.AsyncWaitHandle.WaitOne(1000, true)) { cw("Doing more work in Main"); } // end async int answer = add.EndInvoke(asyncResult); cw("5 + 5 is " + answer); cr(); }
static void Main(string[] args) { Console.WriteLine("[{0}] Main called", Thread.CurrentThread.ManagedThreadId); #if (false) Add(2, 2); #else BinaryOperation binaryOp = Add; //https://msdn.microsoft.com/en-us/library/2e08f6yc(v=vs.110).aspx //https://www.codeproject.com/Articles/10311/What-s-up-with-BeginInvoke //This makes a call to ThreadPool.QueueUserWorkItem and executes the Add method IAsyncResult asyncResult = binaryOp.BeginInvoke(2, 2, null, null); binaryOp.EndInvoke(asyncResult); #endif Console.WriteLine("[{0}] Main Done", Thread.CurrentThread.ManagedThreadId); int i = 20; //i.Saleem(); }
public static void Main(string[] args) { Console.WriteLine("Synch Delegate Review"); Console.WriteLine("Main() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId); BinaryOperation binaryOperation = new BinaryOperation(Add); IAsyncResult asyncResult = binaryOperation.BeginInvoke(10, 10, null, null); while (!asyncResult.AsyncWaitHandle.WaitOne(1000, true)) { Console.WriteLine("Doing more work in Main()!"); Thread.Sleep(1000); } int answer = binaryOperation.EndInvoke(asyncResult); Console.WriteLine("10 + 10 is {0}.", answer); Console.Write("Press any key to continue . . . "); Console.ReadKey(true); }
public static void Main(string[] args) { Console.WriteLine("Synch Delegate Review"); Console.WriteLine("Main() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId); BinaryOperation binaryOperation = new BinaryOperation(Add); IAsyncResult asyncResult = binaryOperation.BeginInvoke(10, 10, new AsyncCallback(AddComplete), "Main() thanks you for adding numbers."); while (!isDone) { Thread.Sleep(1000); Console.WriteLine("Working..."); } Console.Write("Press any key to continue . . . "); Console.ReadKey(true); }