static void Main() { int totalValue = 0; int firstNumber; int secondNumber; Program p = new Program(); Calculator c = new Calculator(); // Get first numeric value. firstNumber = p.GetNumericValue(); // Get second numeric value. secondNumber = p.GetNumericValue(); //Create an instance of the delegate passing in // the Add method of the Calculator class. CalculateMethod cm = new CalculateMethod(c.Add); // TODO: Call the CalculateMethod delegate, named "cm", asynchronously. IAsyncResult result = cm.BeginInvoke(firstNumber, secondNumber, null, null); // TODO: Capture the return value into the variable "totalValue" (declared above) // from the asynchronous call to the CalculateMethod, named "cm". totalValue = cm.EndInvoke(result); Console.WriteLine("\nTotal values: {0}", totalValue); Console.Write("\nPress any key to end."); Console.ReadLine(); }
//线程完成之后回调的函数 public static void TaskFinished(IAsyncResult result) { double re = 0; re = calcMethod.EndInvoke(result); Console.WriteLine(re); }
static void Main() { int totalValue = 0; int firstNumber; int secondNumber; Program p = new Program(); Calculator c = new Calculator(); // Get first numeric value. firstNumber = p.GetNumericValue(); // Get second numeric value. secondNumber = p.GetNumericValue(); // Use delegate method to call the Add method. CalculateMethod cm = new CalculateMethod(c.Add); // Call the Add method asynchronously. IAsyncResult asyncResult = cm.BeginInvoke(firstNumber, secondNumber, null, null); Console.WriteLine("\nWaiting for the task to complete."); //TODO: Use the WaitOne method of the IAsyncResult instance // to force the main thread to wait until the secondary thread // is done executing. Use a time out argument of 5000. // If call completed on time, execute the two code lines below // to display the results. // If call did NOT complete on time, display a message and do not execute // the two code lines below. bool inTimeBool = asyncResult.AsyncWaitHandle.WaitOne(5000); if (inTimeBool) { // Retrieve the return value from the Add method. totalValue = cm.EndInvoke(asyncResult); Console.WriteLine("\nTotal value: {0}", totalValue); } else { Console.WriteLine("\n Call did NOT complete on time. No results. May need to stop application using Task Manager."); } Console.Write("\nPress any key to end."); Console.ReadLine(); }
static void Main() { int totalValue = 0; int firstNumber; int secondNumber; Program p = new Program(); Calculator c = new Calculator(); // Get first numeric value. firstNumber = p.GetNumericValue(); // Get second numeric value. secondNumber = p.GetNumericValue(); // Use delegate method to call the Add method. CalculateMethod cm = new CalculateMethod(c.Add); // Call the Add method asynchronously. IAsyncResult asyncResult = cm.BeginInvoke(firstNumber, secondNumber, null, null); //TODO: Enclose the following code into a loop that is polling to // determine when the asynchronous call is done. After // including the two lines of code below in the loop, then // uncomment them. while (!asyncResult.IsCompleted) { System.Threading.Thread.Sleep(750); Console.WriteLine("\nSeeing if the asynchronous call is done."); } // Retrieve the return value from the Add method. totalValue = cm.EndInvoke(asyncResult); Console.WriteLine("\nTotal value: {0}", totalValue); Console.Write("\nPress any key to end."); Console.ReadLine(); }
///<summary> ///线程完成之后回调的函数 ///<summary> public static void TaskFinished(IAsyncResult asyncResult) { result = calcMethod.EndInvoke(asyncResult); }
public static void TaskFinished(IAsyncResult ret) { double val = cm.EndInvoke(ret); Console.WriteLine("当前返回线程ID:" + Thread.CurrentThread.ManagedThreadId + "委托的返回结果:" + val); }