Exemple #1
0
        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.

            // TODO: Capture the return value into the variable "totalValue" (declared above)
            //      from the asynchronous call to the CalculateMethod, named "cm".

            Console.WriteLine("\nTotal values: {0}",
                totalValue);

            Console.Write("\nPress any key to end.");
            Console.ReadLine();
        }
Exemple #2
0
        static void Main()
        {
            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);

            //TODO:  Create an instance of the callback delegate passing
            //      in the callback method you implemented above.

            // Call the Add method stored in the delegate asynchronously.
            //TODO:  Include the instance of the callback delegate in the
            //      call below.
            IAsyncResult asyncResult = cm.BeginInvoke(firstNumber, secondNumber, null, null);

            System.Threading.Thread.Sleep(2500);

            Console.WriteLine("\nTotal value: {0}",
                p.TotalValue);

            Console.Write("\nPress any key to end.");
            Console.ReadLine();
        }
Exemple #3
0
        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();
        }
Exemple #4
0
        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();
        }
Exemple #5
0
        static void Main()
        {
            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);

            //Create an instance of the callback delegate passing
            //      in the callback method.
            AsyncCallback callbackMethod =
                new AsyncCallback(p.ComputationComplete);

            //Send the beginning time to the callback method.
            DateTime beginTime = DateTime.Now;

            // Call the Add method stored in the delegate asynchronously.
            // TODO: 1. Include in the asychronous call below the instance of
            //      of the callback delegate (callbackMethod) and the
            //      variable (beginTime).
            IAsyncResult asyncResult = cm.BeginInvoke(firstNumber, secondNumber, callbackMethod, beginTime);

            System.Threading.Thread.Sleep(3000);

            Console.WriteLine("\nTotal value: {0}",
                p.TotalValue);

            Console.Write("\nPress any key to end.");
            Console.ReadLine();
        }