Example #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();
        }
Example #2
0
        static void Main(string[] args)
        {
            //1.实例化一个委托,调用者发送一个请求,请求执行该方法体(还未执行)
            DoSomething doSomething = new DoSomething(
                () =>
            {
                Console.WriteLine("如果委托使用 beginInvoke 的话,这里便是异步方法体");
                //4,实现完这个方法体后自动触发下面的回调函数方法体
            }
                );

            //3 。调用者(主线程)去触发异步调用,采用异步的方式请求上面的方法体
            IAsyncResult result = doSomething.BeginInvoke(
                //2.自定义上面方法体执行后的回调函数
                new AsyncCallback
                (
                    //5.以下是回调函数方法体
                    //asyncResult.AsyncState其实就是AsyncCallback委托中的第二个参数
                    asyncResult =>
            {
                doSomething.EndInvoke(asyncResult);
                Console.WriteLine(asyncResult.AsyncState.ToString());
            }
                ),
                "AsyncResult.AsyncState"
                );

            //DoSomething......调用者(主线程)会去做自己的事情
            Console.ReadKey();
        }
Example #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);

                // 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();
        }