示例#1
1
        static void Main(string[] args)
        {
            try
            {
                // Display a message to show we're in Main().
                Console.WriteLine("Starting the program.");

                // Get the number of milliseconds from the arguments
                // passed in from the command line.
                int milliseconds = GetMilliseconds(args[0]);

                // Create the ComplicatedCalculator object.
                ComplicatedCalculator cc =
                    new ComplicatedCalculator(milliseconds);

                // Create the delegate object.
                DoSomething method = new DoSomething(cc.CalculateValue);

                // Call the delegate asynchronously.
                IAsyncResult asynchStatus =
                    method.BeginInvoke(10.4, 7.451, null, null);

                // Call WaitOne() to wait until the async task is done.
                Console.WriteLine("\nWaiting for task to complete.");
                // Include a timeout and check to see if it completed in time.
                bool inTimeBool = asynchStatus.AsyncWaitHandle.WaitOne(10000);
                if (inTimeBool)
                {
                    Console.WriteLine("\nTask has completed.");

                    // Get the result of the asynchronous call.
                    double results = method.EndInvoke(asynchStatus);

                    // Display the results.
                    Console.WriteLine("\nThe result is: {0}", results);
                }
                else
                {
                    Console.WriteLine("\nTask did NOT complete in time. There are no results. May need to stop application running using Task Manager.");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("\nEXCEPTION: {0}.", e.Message);
            }

            // Pause so we can look at the console window.
            Console.Write("\n\nPress <ENTER> to end: ");
            Console.ReadLine();
        }
示例#2
0
        static void Main(string[] args)
        {
            try
            {
                // Display a message to show we're in Main().
                Console.WriteLine("Starting the program.");

                // Get the number of milliseconds from the arguments
                // passed in from the command line.
                int milliseconds = GetMilliseconds(args[0]);

                // Create the ComplicatedCalculator object.
                ComplicatedCalculator cc =
                    new ComplicatedCalculator(milliseconds);

                // Create the delegate object.
                DoSomething method = new DoSomething(cc.CalculateValue);

                // Call the delegate asynchronously.
                IAsyncResult asynchStatus =
                    method.BeginInvoke(10.4, 7.451, null, null);

                // Poll to see if the asynchronous call is done.
                while (!asynchStatus.IsCompleted)
                {
                    System.Threading.Thread.Sleep(750);
                    Console.WriteLine
                        ("\nSeeing if the asynch call is done.");
                }

                // Get the result of the asynchronous call.
                double results = method.EndInvoke(asynchStatus);

                // Display the results.
                Console.WriteLine("\nThe result is: {0}", results);
            }
            catch (Exception e)
            {
                Console.WriteLine("\nEXCEPTION: {0}.", e.Message);
            }

            // Pause so we can look at the console window.
            Console.Write("\n\nPress <ENTER> to end: ");
            Console.ReadLine();
        }
示例#3
0
        static void Main(string[] args)
        {
            try
            {
                // Display a message to show we're in Main().
                Console.WriteLine("Starting the program.");

                // Get the number of milliseconds from the arguments
                // passed in from the command line.
                int milliseconds = GetMilliseconds(args[0]);

                // Create the ComplicatedCalculator object.
                ComplicatedCalculator cc =
                    new ComplicatedCalculator(milliseconds);

                // Create the delegate object.
                DoSomething method = new DoSomething(cc.CalculateValue);

                // Create the callback delegate.
                AsyncCallback callbackMethod =
                    new AsyncCallback(ComputationComplete);

                // Create a string that will be displayed in the
                // callback method.
                string thanksString = "Main() is very happy now!";

                // Call the delegate asynchronously.
                IAsyncResult asynchStatus = method.BeginInvoke
                    (10.4, 7.451, callbackMethod, thanksString);

                // Display some messages to show that Main() is still
                // responsive while the calculation is going on.
                Console.WriteLine("\nNow I'm going to go do something else.");
                System.Threading.Thread.Sleep(1500);
                Console.WriteLine("Like talk about the weather.");
                System.Threading.Thread.Sleep(1500);
                Console.WriteLine("Or the latest news.");
                System.Threading.Thread.Sleep(1500);
                Console.WriteLine("You know, my foot hurts.");
                System.Threading.Thread.Sleep(1500);
                Console.WriteLine("I love hotdogs!");
                System.Threading.Thread.Sleep(1500);
                Console.WriteLine("How much is a shake at Burgermaster?");
                System.Threading.Thread.Sleep(1500);
                Console.WriteLine("Ok, now I'm getting hungry!");
                System.Threading.Thread.Sleep(1500);
            }
            catch (Exception e)
            {
                Console.WriteLine("\nEXCEPTION: {0}.", e.Message);
            }

            // Pause so we can look at the console window.
            Console.Write("\n\nPress <ENTER> to end: ");
            Console.ReadLine();
        }
示例#4
0
        static void Main(string[] args)
        {
            try
            {
                // Display a message to show we're in Main().
                Console.WriteLine("Starting the program.");

                // Get the number of milliseconds from the arguments
                // passed in from the command line.
                int milliseconds = GetMilliseconds(args[0]);

                // Create the ComplicatedCalculator object.
                ComplicatedCalculator cc =
                    new ComplicatedCalculator(milliseconds);

                // Create the delegate object.
                DoSomething method = new DoSomething(cc.CalculateValue);

                // Call the delegate.
                double results = method(10.4, 7.451);

                // Display the results.
                Console.WriteLine("\nThe result is: {0}", results);
            }
            catch (Exception e)
            {
                Console.WriteLine("\nEXCEPTION: {0}.", e.Message);
            }

            // Pause so we can look at the console window.
            Console.Write("\n\nPress <ENTER> to end: ");
            Console.ReadLine();
        }