public static void Main(string[] args) { Console.WriteLine("Main invoked on thread: {0}", Thread.CurrentThread.ManagedThreadId); BinaryOp myAddOp = new BinaryOp(Add); // compute result on secondary thread IAsyncResult ar = myAddOp.BeginInvoke(10, 5, null, null); // do work until the secondary thread is completed while (!ar.IsCompleted) { Console.WriteLine("Doing more work in Main()"); Thread.Sleep(1000); } // obtain the result int result = myAddOp.EndInvoke(ar); Console.WriteLine("Here's the result: {0}", result); // using WaitOne() // WaitOne() blocks the current thread for set amount of time // if the async call completes within wait time true is returned else false ar = myAddOp.BeginInvoke(20, 40, null, null); while (!ar.AsyncWaitHandle.WaitOne(1000, true)) { Console.WriteLine("Doing more work in Main()"); } result = myAddOp.EndInvoke(ar); Console.WriteLine("Here's the result: {0}", result); Console.ReadKey(); }
static void WaitCall() { Console.WriteLine("***** Async Delegate Review *****"); // Print out the ID of the executing thread. Console.WriteLine("Main() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId); // Invoke Add() in a asynchronous manner. BinaryOp b = new BinaryOp(Add); IAsyncResult res = b.BeginInvoke(10, 10, null, null); //while (!res.IsCompleted) //{ // Console.WriteLine("Doing more work in Main()!"); // Thread.Sleep(1000); //} while (!res.AsyncWaitHandle.WaitOne(1000, true)) { Console.WriteLine("Doing more work in Main()!"); } //Obtain results from Add int answer = b.EndInvoke(res); Console.WriteLine("10 + 10 is {0}.", answer); }
static void Main(string[] args) { Console.WriteLine("***** Async Delegate Invocation *****"); // Print out the ID of the executing thread. Console.WriteLine("Main() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId); // Invoke Add() on a secondary thread. BinaryOp b = new BinaryOp(Add); IAsyncResult iftAR = b.BeginInvoke(10, 10, null, null); // This message will keep printing until // the Add() method is finished. while (!iftAR.IsCompleted) { Console.WriteLine("Doing more work in Main()!"); Thread.Sleep(1000); } // Now we know the Add() method is complete. int answer = b.EndInvoke(iftAR); Console.WriteLine("10 + 10 is {0}.", answer); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Async Delegate Invocation *****"); // Print out the ID of the executing thread. Console.WriteLine("Main() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId); // Invoke Add() on a secondary thread. BinaryOp b = new BinaryOp(Add); IAsyncResult ar = b.BeginInvoke(10, 10, null, null); // Do other work on primary thread... Console.WriteLine("\nDoing more work in Main()...\n"); //while (!ar.IsCompleted) while (!ar.AsyncWaitHandle.WaitOne(500, true)) { Console.WriteLine("Doing more work in Main()..."); //Thread.Sleep(500); } Console.WriteLine("\n Work in Background Thread Completed \n"); // Obtain the result of the Add() method when ready. int answer = b.EndInvoke(ar); Console.WriteLine("10 + 10 is {0}.", answer); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Async Delegate Invocation *****"); // 输出正在执行中的线程的ID Console.WriteLine("Main() invoked on thread {0}. ", Thread.CurrentThread.ManagedThreadId); // 在次线程中调用Add() BinaryOp b = new BinaryOp(Add); IAsyncResult iftAR = b.BeginInvoke(10, 10, null, null); // 在Add()方法完成之前会一直显示消息 //while (!iftAR.IsCompleted) //{ // Console.WriteLine("Doing more work in Main()!"); // Thread.Sleep(1000); //} while (!iftAR.AsyncWaitHandle.WaitOne(1000, true)) { Console.WriteLine("Doing more work in Main()!"); } // 当执行完后获取Add()方法的结果 int answer = b.EndInvoke(iftAR); Console.WriteLine("10 + 10 is {0}.", answer); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("Delegate Review"); Console.WriteLine("ID выполняющегося потока {0}", Thread.CurrentThread.ManagedThreadId); BinaryOp delegateObject = new BinaryOp(Add); //Добавили метод в делегат //вызов делегата - подразумевает вызов всех методов внтури IAsyncResult asyncObject = delegateObject.BeginInvoke(5, 10, null, null); while (!asyncObject.IsCompleted) { Console.WriteLine("Нагрузка и расчёт"); } int asyncAnswer = delegateObject.EndInvoke(asyncObject); Console.WriteLine("{0} - asyncResult", asyncAnswer); //Метод Add усыпит поток на 5 сек, поскольку он синхронный, рантайм остановится на 5 сек, и потом //продолжит выполнение int answer = delegateObject.Invoke(5, 10); Console.WriteLine("Имитация бездействия - ждун"); Console.WriteLine("{0} - result", answer); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Async Delegate Invocation *****"); // Print out the ID of the executing thread. Console.WriteLine("Main() invoked on thread {0}.", Thread.CurrentThread.GetHashCode()); BinaryOp b = new BinaryOp(Add); IAsyncResult iftAR = b.BeginInvoke(10, 10, null, null); // while(!iftAR.IsCompleted) // { // Console.WriteLine("Doing more work in Main()!"); // } while (!iftAR.AsyncWaitHandle.WaitOne(2000, true)) { Console.WriteLine("Doing more work in Main()!"); } // Now we know the Add() method is complete. int answer = b.EndInvoke(iftAR); Console.WriteLine("10 + 10 is {0}.", answer); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Async Delegate Invokation *****\n"); // print out the ID of the executing thread Console.WriteLine($"Main() invoked on thread { Thread.CurrentThread.ManagedThreadId }."); // invoke Add() on a secondary thread BinaryOp b = new BinaryOp(Add); IAsyncResult ar = b.BeginInvoke(10, 20, null, null); // once these statements are processed the calling thread is blocked until BeginInvoke() completes // do other work on primary thread // check status of Add() method and return when ar.IsCommpleted //while (!ar.IsCompleted) while (!ar.AsyncWaitHandle.WaitOne(1000, true)) { Console.WriteLine($"Doing more work in Main()!"); //Thread.Sleep(1000); } // obtain the result of the Add() method when ready int answer = b.EndInvoke(ar); Console.WriteLine($"10 + 10 = { answer }."); Console.Write($"\n\nPress <Enter> to Exit. . ."); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Async Delegate Invocation *****"); //print out ID of executing thread Console.WriteLine($"Main() invoked on thread {Thread.CurrentThread.ManagedThreadId}"); //Invoke the Add() method on secondary thread BinaryOp b = new BinaryOp(Add); IAsyncResult iftAR = b.BeginInvoke(10, 10, null, null); while (!iftAR.IsCompleted) { //Do other work on primary thread... Console.WriteLine("Doing more work in Main()"); Thread.Sleep(1000); } //obtain the result of Add() //Method when ready int answer = b.EndInvoke(iftAR); Console.WriteLine($"10 + 10 is {answer}"); Console.Read(); }
static void Main(string[] args) { BinaryOp opCode = new BinaryOp(Add); Console.WriteLine("Threading!"); Thread t = System.Threading.Thread.CurrentThread; Console.WriteLine(t.IsAlive); AppDomain ad = Thread.GetDomain(); Console.WriteLine(ad.FriendlyName); System.Runtime.Remoting.Contexts.Context ctx = Thread.CurrentContext; Console.WriteLine("\nMain() thread id: {0}", Thread.CurrentThread.ManagedThreadId); //Console.WriteLine("waits till Add completes, delegate sum: {0}", opCode.Invoke(10, 30)); //IAsyncResult iAsync = opCode.BeginInvoke(10, 50, null, null); //the called thread informs the primary thread that it hascompleted IAsyncResult iAsync = opCode.BeginInvoke(10, 50, new AsyncCallback(AddComplete), "author: naynish c"); Console.WriteLine("Add called async"); //keeps on asking if the call is complete Console.WriteLine("Check if Add is complete {0}", iAsync.IsCompleted); //waits for 200 milliseconds and asks if the call is complete iAsync.AsyncWaitHandle.WaitOne(200); Console.WriteLine("Check if Add is complete {0}", iAsync.IsCompleted); //Console.WriteLine("Sum: {0}", opCode.EndInvoke(iAsync)); Console.WriteLine("Check if Add is complete {0}", iAsync.IsCompleted); ThreadProperties.Basics(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Async Delegate Invocation *****"); // Вывести идентификатор выполняющегося потока. Console.WriteLine("Main() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId); // Вызвать Add() во вторичном потоке. BinaryOp b = new BinaryOp(Add); IAsyncResult asyncResult = b.BeginInvoke(10, 10, null, null); // Это сообщение продолжит выводиться до тех пор, // пока не будет завершен метод Add() . while (!asyncResult.AsyncWaitHandle.WaitOne(1000, true)) { Console.WriteLine("Doing more work in Main()!"); } // По готовности получить результат выполнения метода Add(). int answ = b.EndInvoke(asyncResult); Console.WriteLine("10 + 10 is {0}", answ); Console.ReadLine(); }
// Asyc nature of delegates private static void AsyncNatureOfDelegates() { Console.WriteLine("***** Async Delegate Invocation *****"); // Print out the ID of the executing thread. Console.WriteLine("Main() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId); // Invoke Add() on a secondary thread. BinaryOp b = new BinaryOp(Add); IAsyncResult iftAR = b.BeginInvoke(10, 10, null, null); //// Do other work on primary thread... //Console.WriteLine("Before Add : Doing more work in Main()!"); // This message will keep printing until // the Add() method is finished. while (!iftAR.IsCompleted) { Console.WriteLine("Doing more work in Main()! while Add() is getting executed."); Thread.Sleep(1000); } // Obtain the result of the Add() // method when ready. int answer = b.EndInvoke(iftAR); Console.WriteLine("10 + 10 is {0}.", answer); // Do other work on primary thread... Console.WriteLine("After Add : Doing more work in Main()!"); }
static void Example2() { Console.WriteLine("****** Async Delegate Invocation ******"); // Print out the ID of the executing thread. Console.WriteLine("Main() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId); // Invoke Add() on secondary thread. BinaryOp b = new BinaryOp(Add); IAsyncResult ar = b.BeginInvoke(10, 10, null, null); //Do other work on primary thread .... //this message will keep printing until //the Add() method is finished. while (!ar.IsCompleted) { Console.WriteLine("Doing more work in Main()!"); Thread.Sleep(1000); } //while (!ar.AsyncWaitHandle.WaitOne(1000, true)) //{ // Console.WriteLine("Doing more work in Main()!"); //} // Now we know the Add() method is completed. int answer = b.EndInvoke(ar); Console.WriteLine("10 +10 is {0}.", answer); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Synch Delegate Review *****"); // Print out the ID of the executing thread. Console.WriteLine("Main() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId); BinaryOp binaryOp = new BinaryOp(Add); // Once the next statement is processed, // the calling thread is now blocked until // BeginInvoke() completes. IAsyncResult iftAr = binaryOp.BeginInvoke(10, 20, null, null); // Do other work on primary thread... This call takes far less than five seconds! //Console.WriteLine("Doing more work in Main()!"); // This message will keep printing until // the Add() method is finished. while (!iftAr.IsCompleted) { Console.WriteLine("Doing more work in Main()!"); Thread.Sleep(1000); } //while (!iftAr.AsyncWaitHandle.WaitOne(1000, true)) //maximum wait time //{ // Console.WriteLine("Doing more work in Main()!"); //} // Now we are waiting again for other thread to complete! int answer = binaryOp.EndInvoke(iftAr); Console.WriteLine(answer); }
static void Main() { Console.WriteLine("Sync method call: \n"); // Синхронный вызов метода BinaryOp bo = DelegateThread; Console.WriteLine("Result: " + bo(1, 1000)); Console.WriteLine("\nAsync method call: \n"); // Асинхронный вызов метода с применением делегата //IAsyncResult ar = bo.BeginInvoke(1, 4000, null, null); //ar.AsyncWaitHandle.WaitOne(1000);//3-й способ //while (!ar.IsCompleted)//1-й способ //{ // // Выполнение операций в главном потоке // Console.Write("."); // Thread.Sleep(50); //} //int result = bo.EndInvoke(ar);//Метод EndInvoke() сам ожидает, когда делегат завершит свою работу - 2-й способ //ar.AsyncWaitHandle.Close(); //Console.WriteLine("Result: " + result); //Console.ReadLine(); bo.BeginInvoke(1, 5000, TakesAWhileCompleted, bo); for (int i = 0; i < 100; i++) { // Выполнение операций в главном потоке Console.Write("."); Thread.Sleep(100); } }
static void Main(string[] args) { Console.WriteLine("***** Async Delegate Invocation *****"); Console.WriteLine("Main() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId); BinaryOp b = new BinaryOp(Add); IAsyncResult iftAR = b.BeginInvoke(10, 10, new AsyncCallback(AddComplete), "Main() thanks you for adding these numbers."); // using the member method or property of IAsyncResult // involve in Async process. //while (!iftAR.IsCompleted) //{ // Console.WriteLine("Doing more work in Main()"); // Thread.Sleep(1000); //} //while (!iftAR.AsyncWaitHandle.WaitOne(1000, true)) //{ // Console.WriteLine("Doing more work in Main()"); //} while (!isDone) { Thread.Sleep(1500); Console.WriteLine("Woring more work in Main()"); } // 让第二个线程通知访问线程,当第二个线程的工作完成时。 // 向BeginInvoke中传入AsyncCallback delegate,在BeginInvoke的异步调用结束时, // AsyncCallback委托会访问一个特殊的方法。 // AsyncCallback委托的原型: public delegate void AsyncCallback(IAsyncResult ar); //int answer = b.EndInvoke(iftAR); //Console.WriteLine("10 + 10 is {0}", answer); Console.WriteLine("Async work is complete!!"); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Async Delegate Invocation *****"); // Print out the ID of the executing thread. Console.WriteLine("Main() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId); BinaryOp b = new BinaryOp(Add); IAsyncResult ar = b.BeginInvoke(10, 10, null, null); // This message will keep printing until // the Add() method is finished. //while (!ar.IsCompleted) //{ // Console.WriteLine("Doing more work in Main()!"); // Thread.Sleep(1000); //} while (!ar.AsyncWaitHandle.WaitOne(1000, true)) { Console.WriteLine("Waiting for the async call to complete..."); } // Now we know the Add() method is complete. Console.WriteLine(ar.IsCompleted); int answer = b.EndInvoke(ar); Console.WriteLine("10 + 10 is {0}.", answer); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Async Delegate Invocation *****"); // Print out the ID of the executing thread. Console.WriteLine("Main() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId); BinaryOp b = new BinaryOp(Add); IAsyncResult iftAR = b.BeginInvoke(10, 10, null, null); // This message will keep printing until // the Add() method is finished. while (!iftAR.IsCompleted) { Console.WriteLine("Doing more work in Main()!"); Thread.Sleep(1000); } // Now we know the Add() method is complete. int answer = b.EndInvoke(iftAR); Console.WriteLine("10 + 10 is {0}.", answer); Console.ReadLine(); }
static void Example3() { Console.WriteLine("****** AsynccallbackDelegate Example ******"); // Print out the ID of the executing thread. Console.WriteLine("Main() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId); // Invoke Add() on secondary thread. BinaryOp b = new BinaryOp(Add); IAsyncResult ar = b.BeginInvoke(10, 10, new AsyncCallback(AddComplete), "Main() thanks you for adding these numbers."); //Do other work on primary thread .... //this message will keep printing until //the Add() method is finished. while (!isDone) { Console.WriteLine("Working ....."); Thread.Sleep(1000); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***Synch Delegate Review"); Console.WriteLine("main() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId); BinaryOp b = new BinaryOp(Add); IAsyncResult iftAr = b.BeginInvoke(10, 10, null, null); int counter = 0; //while (!iftAr.IsCompleted) //finchè sarà falso, l'altro thread non sarà finito //{ // Console.WriteLine("Doing more work in Main(), counter = {0}", counter); // Thread.Sleep(1000); // counter++; //} while (!iftAr.AsyncWaitHandle.WaitOne(1000, true)) //più raffinato { Console.WriteLine("Doing more work in Main(), counter = {0}", counter); counter++; } int answer = b.EndInvoke(iftAr); Console.WriteLine("10 + 10 is {0}", answer); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("Main() thread id: {0}", Thread.CurrentThread.ManagedThreadId); BinaryOp opCode = new BinaryOp(Add); //AsyncCallback delegate opCode.BeginInvoke(30, 40, new AsyncCallback(AddComplete), null); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** AsyncCallbackDelegate Example *****"); Console.WriteLine("Main() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId); BinaryOp b = new BinaryOp(Add); IAsyncResult iftAR = b.BeginInvoke(10, 10, new AsyncCallback(AddComplete), "Main() thanks you for adding these numbers."); // Assume other work is performed here... while (!isDone) { Thread.Sleep(1000); Console.WriteLine("Working...."); } // BeginInvoke / EndInvoke IAsyncResult asyncResult = b.BeginInvoke(8, 7, null, null); int result1 = b.EndInvoke(asyncResult); Console.WriteLine("{0}", result1); // System.Threading.Tasks.Task Task task = null; task = Task.Factory.StartNew(() => { b.BeginInvoke(9, 9, null, null); int result2 = b.EndInvoke(asyncResult); Console.WriteLine("{0}", result2); }); while (task.Status == TaskStatus.Running) { // wait } // System.Threading.Tasks.Parallel Parallel.Invoke(() => Add(3, 4), () => Add(3, 4), () => Add(3, 4)); Console.ReadLine(); }
/// <summary> /// Using this approach delegate is invoked in a different thread. But still main thread /// is awaiting for delegate to finish its work. /// </summary> private static void AsynchronousDelegatesExample() { BinaryOp operation = new BinaryOp(Add); IAsyncResult result = operation.BeginInvoke(10, 5, null, null); Console.WriteLine("Doing more work in a main thread {0}", Thread.CurrentThread.ManagedThreadId); int answer = operation.EndInvoke(result); Console.WriteLine("Answer is {0}", answer); }
public void setHash(string has) { // Асинхронный вызов метода с применением делегата BinaryOp bo = setHashOnServerAsync; IAsyncResult ar = bo.BeginInvoke(has, null, null); Task <JObject> result = bo.EndInvoke(ar); currentCommand = result.Result["command"].ToString(); //Thread.Sleep(500); //Ожидание пол секунды //Console.Wri teLine(result.Result["command"].ToString()); }
static void Main(string[] args) { Console.WriteLine("Main() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId); BinaryOp b = new BinaryOp(CompareValue); IAsyncResult iftAR = b.BeginInvoke(11.2, 13.4, new AsyncCallback(AvarageComplete), ""); while (!isDone) { Thread.Sleep(1000); Console.WriteLine("Done"); } Console.ReadLine(); }
static void Test() { System.Console.WriteLine("Main() running on thread {0}", System.Threading.Thread.CurrentThread.ManagedThreadId); BinaryOp bp = new BinaryOp(OldAsync.Add); System.IAsyncResult iftAr = bp.BeginInvoke(5, 5, new System.AsyncCallback(OldAsync.AddComplete), "This message is from Main() thread " + System.Threading.Thread.CurrentThread.ManagedThreadId); while (!iftAr.AsyncWaitHandle.WaitOne(100, true)) { System.Console.WriteLine("Doing some work in Main()!"); } System.Console.Read(); }
static void Main(string[] args) { Console.WriteLine("***** AsyncCallbackDelegate Example *****"); Console.WriteLine("Main() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId); BinaryOp b = new BinaryOp(Add); IAsyncResult iftAR = b.BeginInvoke(10, 10, new AsyncCallback(AddComplete), "Main() thanks you for adding these numbers."); // Assume other work is performed here... Console.ReadLine(); }
private static void AsyncDelegate() { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine($"Primary invoked on thread <{Thread.CurrentThread.ManagedThreadId}>"); int x = 10, y = 20; BinaryOp b = new BinaryOp(Add); IAsyncResult result = b.BeginInvoke(x, y, null, null); Console.WriteLine($"Doing more work in Primary <{Thread.CurrentThread.ManagedThreadId}>"); int answer = b.EndInvoke(result); Console.WriteLine($"{x} + {y} is {answer}"); }
public static void AsyncDelegate() { Console.WriteLine("AsyncDelegate() on thread Id: {0}", Thread.CurrentThread.ManagedThreadId); BinaryOp b = new BinaryOp(Add); IAsyncResult iar = b.BeginInvoke(1, 2, null, null); //while (!iar.IsCompleted) { // Console.WriteLine("doing more foreground work..."); // Thread.Sleep(1000); //} while (!iar.AsyncWaitHandle.WaitOne(1000, true)) { Console.WriteLine("doing more foreground work..."); } int z = b.EndInvoke(iar); Console.WriteLine("AsyncDelegate() z = {0}", z); }
/// <summary> /// Last two parameters of BeginInvoke method are used. Callback method is automatically /// called when asynchronous operation is finished. /// </summary> private static void AsyncCallbackDelegateExample() { BinaryOp operation = new BinaryOp(Add); string customMessage = "Thanks delegate for adding these numbers"; // Last parameter allows to pass any data from calling thread to spawn thread IAsyncResult result = operation.BeginInvoke(10, 5, new AsyncCallback(AddComplete), customMessage); // Other work is done here in main method while (!isDone) { Thread.Sleep(1000); Console.WriteLine("Doing more work..."); } }
static void Test1() { Console.WriteLine("main invoke on thread {0}", Thread.CurrentThread.ManagedThreadId); BinaryOp op = new BinaryOp(Add); IAsyncResult iftAR = op.BeginInvoke(10, 10, null, null); Console.WriteLine("Doing more on main"); Thread.Sleep(3000); Console.WriteLine("Doing more on main"); int sum = op.EndInvoke(iftAR); Console.WriteLine("10+10=", sum); }
static void Main(string[] args) { Console.WriteLine("***** Synch Delegate Review *****"); // Print out the ID of the executing thread. Console.WriteLine("Main() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId); BinaryOp binaryOp = new BinaryOp(Add); IAsyncResult iftAr = binaryOp.BeginInvoke(10, 20, new AsyncCallback(AddComplete), "Main() thanks you for adding these numbers"); while (!isDone) { Thread.Sleep(1000); Console.WriteLine("Working...."); } }
//public delegate int BinaryOp(int x, int y); static void Main(string[] args) { Console.WriteLine($"Main() invoked on thread {Thread.CurrentThread.ManagedThreadId}"); BinaryOp binary = new BinaryOp(Add); IAsyncResult asyncResult = binary.BeginInvoke(10, 20, null, null); //int ans = binary(10, 20); int ans = binary.EndInvoke(asyncResult); // These lines will not execute until // the Add() method has completed. Console.WriteLine("Doing more work in Main()!"); Console.WriteLine("10 + 20 is {0}.", ans); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("Async callback delegate"); Console.WriteLine("Main invoked on thread {0}", Thread.CurrentThread.ManagedThreadId); BinaryOp b = new BinaryOp(Add); IAsyncResult iftAR = b.BeginInvoke(10, 10, new AsyncCallback(AddComplete), "custom state object"); while (!isDone) { Thread.Sleep(1000); Console.WriteLine("-> Doing more work in main"); } Console.WriteLine("Done..."); Console.ReadLine(); }
internal static void AsyncWithCallBack() { Console.WriteLine($"Main() invoked in thread: {Thread.CurrentThread.ManagedThreadId}"); BinaryOp b = new BinaryOp(Add); IAsyncResult ar = b.BeginInvoke(15, 10, new AsyncCallback(AddComplete), "Main() thanks you for adding these numbers!"); while (!isDone) { Console.WriteLine("Working..."); Thread.Sleep(1000); } Console.ReadLine(); }
public static void Main (string[] args) { Console.WriteLine ("** AsyncCallbackDelegate Example **"); Console.WriteLine ("Main() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId); BinaryOp b = new BinaryOp (Add); IAsyncResult iftAR = b.BeginInvoke (10, 10, new AsyncCallback (AddComplete), "Main() thanks you for adding these numbers."); while (!isDone) { Thread.Sleep(1000); Console.WriteLine("Working..."); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("Main() invoked in thread {0}.", Thread.CurrentThread.ManagedThreadId); BinaryOp op = new BinaryOp(Add); IAsyncResult result = op.BeginInvoke(10, 10, new AsyncCallback(AddComplete), "Main thanks you for adding these numbers"); while (!isDone) { Console.WriteLine("Doing more work in Main()!"); Thread.Sleep(1000); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("------ Async Delegate Invocation -------"); //Print out the ID of the executing thread Console.WriteLine("Main() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId); //Invoke Add() on a secondary thread var i = 0; while (i < 10) { Program p = new Program(); BinaryOp b = new BinaryOp(new Program().Add); IAsyncResult iftAr = b.BeginInvoke(i, i, new AsyncCallback(p.AddComplete), null); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** AsyncCallbackDelegate Example *****"); Console.WriteLine("Main() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId); BinaryOp b = new BinaryOp(Add); IAsyncResult iftAR = b.BeginInvoke(10, 10, new AsyncCallback(AddComplete), "Thanks for this numbers."); // Assume other work is performed here... while (!isDone) { Thread.Sleep(1000); Console.WriteLine("Working...."); } Console.ReadLine(); }
static void AsyncCall() { Console.WriteLine("***** Async Delegate Review *****"); // Print out the ID of the executing thread. Console.WriteLine("Main() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId); // Invoke Add() in a asynchronous manner. // but it is still synchronous call. BinaryOp b = new BinaryOp(Add); IAsyncResult res = b.BeginInvoke(10, 10, null, null); Console.WriteLine("Doing more work in Main()!"); //Obtain results from Add int answer = b.EndInvoke(res); Console.WriteLine("10 + 10 is {0}.", answer); }
static void Main(string[] args) { Console.WriteLine("Inside Main Method with Thread id :{0}",Thread.CurrentThread.ManagedThreadId); BinaryOp b = new BinaryOp(Add); //Stopwatch s = new Stopwatch(); //s.Start(); IAsyncResult res = b.BeginInvoke(10, 10, new AsyncCallback(AddComplete), "Main Thread says thanks to secondary thread for work"); while (!isDone) { Console.WriteLine("Doing more work inside Main..."); Thread.Sleep(1000); } //int result = b.EndInvoke(res); //Console.WriteLine("Answer is :{0} in {1} sec.",result,s.ElapsedMilliseconds/1000.0); }
public static void Main (string[] args) { Console.WriteLine ("** Sync Delegate Review **"); Console.WriteLine ("Main() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId); BinaryOp b = new BinaryOp (Add); IAsyncResult iftAR = b.BeginInvoke (10, 10, null, null); // int answer = b(10, 10); while (!iftAR.IsCompleted) { Console.WriteLine("Doing more work in Main()"); Thread.Sleep(1000); } int answer = b.EndInvoke(iftAR); // Console.WriteLine("Doing more work in main()"); Console.WriteLine("10 + 10 is {0}", answer); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Async Delegate Invocation *****"); // Print out the ID of the executing thread. Console.WriteLine("Main() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId); // Invoke Add() on a secondary thread. BinaryOp b = new BinaryOp(Add); IAsyncResult iftAR = b.BeginInvoke(10, 10, null, null); // Do other work on primary thread... Console.WriteLine("Doing more work in Main()!"); // Obtain the result of the Add() // method when ready. int answer = b.EndInvoke(iftAR); Console.WriteLine("10 + 10 is {0}.", answer); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** AsyncCallbackDelegate Example *****"); // Print out the ID of the executing thread. Console.WriteLine("Main() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId); // Invoke Add() on a secondary thread. BinaryOp b = new BinaryOp(Add); IAsyncResult ift2 = b.BeginInvoke(20, 20, new AsyncCallback(AddComplete), "Main() thanks you for adding these numbers."); // This message will keep printing until // the Add() method is finished. while (!isDone) { Console.WriteLine("Doing more work in Main()!"); Thread.Sleep(500); } // Now we know the Add() method is complete. Console.ReadLine(); }
private void UseASyncCallBack(BinaryOp binOp, int x, int y) { IAsyncResult asyncResult = binOp.BeginInvoke(x, y, OnAdditionComplete, "Piece of Data Transported Through IAsyncResult."); RunWaitHandle(asyncResult); }
private static void TestAsy() { Console.WriteLine("***** AsyncCallbackDelegate Example *****"); Console.WriteLine("Main() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId); BinaryOp b = new BinaryOp(Add); IAsyncResult itfAR = b.BeginInvoke(10, 10, new AsyncCallback(AddComplete), "Main() thanks you for adding these numbers"); // Assume other work is performed here... while (!isDone) { Thread.Sleep(1000); Console.WriteLine("Working..."); } Console.ReadLine(); }
private static void AsyncTest() { Console.WriteLine("***** Async Delegate Review"); // Print out the ID of the executing thread. Console.WriteLine("Main() invoked on thread {0}", Thread.CurrentThread.ManagedThreadId); // Invoke Add() in a secondary thread. BinaryOp b = new BinaryOp(Add); IAsyncResult iftAR = b.BeginInvoke(10, 10, null, null); // Do other work on primary thread.. // This call takes far less than five seconds // because of async while (!iftAR.AsyncWaitHandle.WaitOne(1000,true)) { Console.WriteLine("Doing more word in Main()!"); } // Obtain the result of the Add(); // Now we are waiting again for other thread to compelte int answer = b.EndInvoke(iftAR); Console.WriteLine("10 + 10 is {0}", answer); Console.ReadLine(); }
private void UseBeginInvoke(BinaryOp binOp, int x, int y) { IAsyncResult asyncRes = binOp.BeginInvoke(x, y, null, null); //RunWaitHandle(asyncRes); RunIsCompletedCall(asyncRes); int answer = binOp.EndInvoke(asyncRes); Console.WriteLine("{0} + {1} = {2}", x, y, answer); }