Exemple #1
0
        static void Main()
        {
            // The asynchronous method puts the thread id here.
            int threadId;

            // Create an instance of the test class.
            AsyncDemo ad = new AsyncDemo();

            // Create the delegate.
            AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);

            // Initiate the asychronous call.
            IAsyncResult result = caller.BeginInvoke(3000,
                                                     out threadId, null, null);

            // Poll while simulating work.
            while (result.IsCompleted == false)
            {
                Thread.Sleep(250);
                Console.Write(".");
            }

            // Call EndInvoke to retrieve the results.
            string returnValue = caller.EndInvoke(out threadId, result);

            Console.WriteLine("\nThe call executed on thread {0}, with return value \"{1}\".",
                              threadId, returnValue);
        }
Exemple #2
0
        static void Main()
        {
            // The asynchronous method puts the thread id here.
            int threadId;

            // Create an instance of the test class.
            AsyncDemo ad = new AsyncDemo();

            // Create the delegate.
            AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);

            // Initiate the asychronous call.
            IAsyncResult result = caller.BeginInvoke(3000,
                                                     out threadId, null, null);

            Thread.Sleep(0);
            Console.WriteLine("Main thread {0} does some work.",
                              Thread.CurrentThread.ManagedThreadId);

            // Wait for the WaitHandle to become signaled.
            result.AsyncWaitHandle.WaitOne();

            // Perform additional processing here.
            // Call EndInvoke to retrieve the results.
            string returnValue = caller.EndInvoke(out threadId, result);

            // Close the wait handle.
            result.AsyncWaitHandle.Close();

            Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".",
                              threadId, returnValue);
        }
Exemple #3
0
        public static void Main()
        {
            // The asynchronous method puts the thread id here.
            int threadId;

            // Create an instance of the test class.
            AsyncDemo ad = new AsyncDemo();

            // Create the delegate.
            AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);

            // Initiate the asychronous call.
            IAsyncResult result = caller.BeginInvoke(3000,
                                                     out threadId, null, null);

            Thread.Sleep(0);
            Console.WriteLine("Main thread {0} does some work.",
                              Thread.CurrentThread.ManagedThreadId);

            // Call EndInvoke to wait for the asynchronous call to complete,
            // and to retrieve the results.
            string returnValue = caller.EndInvoke(out threadId, result);

            Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".",
                              threadId, returnValue);
        }
        static void Main()
        {
            // The asynchronous method puts the thread id here.
            int threadId;

            // Create an instance of the test class.
            AsyncDemo ad = new AsyncDemo();

            // Create the delegate.
            AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);

            // Initiate the asychronous call.
            IAsyncResult result = caller.BeginInvoke(3000, out threadId, null, null);

            // Poll while simulating work.
            while (result.IsCompleted == false)
            {
                Thread.Sleep(250);
                Console.Write(".");
            }

            // Call EndInvoke to retrieve the results.
            string returnValue = caller.EndInvoke(out threadId, result);

            Console.WriteLine("\nThe call executed on thread {0}, with return value \"{1}\".", threadId, returnValue);
        }
Exemple #5
0
        static void Main()
        {
            // The asynchronous method puts the thread id here.
            int threadId;

            // Create an instance of the test class.
            AsyncDemo ad = new AsyncDemo();

            // Create the delegate.
            AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);

            // #3 Initiate the asynchronous call, passing three seconds (3000 ms)
            // for the callDuration parameter of TestMethod; a dummy variable
            // for the out parameter (threadId); the callback delegate; and
            // state information that can be retrieved by the callback method.
            // In this case, the state information is a string that can be used
            // to format a console message.
            //IAsyncResult result = caller.BeginInvoke(3000,
            //    out threadId,
            //    new AsyncCallback(CallbackMethod),
            //    "The call executed on thread {0}, with return value \"{1}\".");


            // Initiate the asychronous call.
            IAsyncResult result = caller.BeginInvoke(3000,
                                                     out threadId, null, null);

            // After calling BeginInvoke you can do the following:
            // Do some work and then call EndInvoke to block until the call completes.
            // Obtain a WaitHandle using the IAsyncResult.AsyncWaitHandle property, use its WaitOne method to block execution until the WaitHandle is signaled, and then call EndInvoke.
            // Poll the IAsyncResult returned by BeginInvoke to determine when the asynchronous call has completed, and then call EndInvoke.
            // Pass a delegate for a callback method to BeginInvoke. The method is executed on a ThreadPool thread when the asynchronous call completes. The callback method calls EndInvoke.

            Thread.Sleep(0);
            Console.WriteLine("Main thread {0} does some work.",
                              Thread.CurrentThread.ManagedThreadId);

            // #1 Wait for the WaitHandle to become signaled.
            result.AsyncWaitHandle.WaitOne();

            // #2 Poll while simulating work.
            while (result.IsCompleted == false)
            {
                Thread.Sleep(250);
                Console.Write(".");
            }

            // Perform additional processing here.

            // Call EndInvoke to retrieve the results. Will wait if you didn't do that previously.
            string returnValue = caller.EndInvoke(out threadId, result);

            // Close the wait handle.
            result.AsyncWaitHandle.Close();

            Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".",
                              threadId, returnValue);
        }
Exemple #6
0
        static void Main()
        {
            // Create an instance of the test class.
            AsyncDemo ad = new AsyncDemo();

            // Create the delegate.
            AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);

            // The threadId parameter of TestMethod is an out parameter, so
            // its input value is never used by TestMethod. Therefore, a dummy
            // variable can be passed to the BeginInvoke call. If the threadId
            // parameter were a ref parameter, it would have to be a class-
            // level field so that it could be passed to both BeginInvoke and
            // EndInvoke.
            int dummy = 0;

            // Initiate the asynchronous call, passing three seconds (3000 ms)
            // for the callDuration parameter of TestMethod; a dummy variable
            // for the out parameter (threadId); the callback delegate; and
            // state information that can be retrieved by the callback method.
            // In this case, the state information is a string that can be used
            // to format a console message.
            IAsyncResult result = caller.BeginInvoke(3000,
                                                     out dummy,
                                                     new AsyncCallback(CallbackMethod),
                                                     "The call executed on thread {0}, with return value \"{1}\".");

            Console.WriteLine("The main thread {0} continues to execute...",
                              Thread.CurrentThread.ManagedThreadId);

            // The callback is made on a ThreadPool thread. ThreadPool threads
            // are background threads, which do not keep the application running
            // if the main thread ends. Comment out the next line to demonstrate
            // this.
            Thread.Sleep(4000);

            Console.WriteLine("The main thread ends.");
        }
Exemple #7
0
        public static bool Main2()
        {
            // The asynchronous method puts the thread id here.
            int threadId;

            // Create an instance of the test class.
            AsyncDemo ad = new AsyncDemo();

            // Create the delegate.
            AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);

            // Initiate the asychronous call.
            IAsyncResult result = funnyhelper(caller, out threadId);

            Thread.Sleep(0);

            // Call EndInvoke to wait for the asynchronous call to complete,
            // and to retrieve the results.
            string returnValue = caller.EndInvoke(out threadId, result);

            bool flag = (threadId == Convert.ToInt32(returnValue));

            return(flag);
        }
Exemple #8
0
        static void Main(string[] args)
        {
            int               threadId;
            AsyncDemo         ad     = new AsyncDemo();
            AsyncMethodCaller caller = ad.TestMethod;

            IAsyncResult result = caller.BeginInvoke(3000, out threadId, null, null);

            Thread.Sleep(0);
            Console.WriteLine("Main thread {0} does some work.", Thread.CurrentThread.ManagedThreadId);

            result.AsyncWaitHandle.WaitOne();

            string returnValue = caller.EndInvoke(out threadId, result);


            result.AsyncWaitHandle.Close();


            Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".",
                              threadId, returnValue);

            Console.ReadLine();
        }
Exemple #9
0
        static void Main()
        {
            // Create an instance of the test class.
            AsyncDemo ad = new AsyncDemo();

            // Create the delegate.
            AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);

            // The threadId parameter of TestMethod is an out parameter, so
            // its input value is never used by TestMethod. Therefore, a dummy
            // variable can be passed to the BeginInvoke call. If the threadId
            // parameter were a ref parameter, it would have to be a class-
            // level field so that it could be passed to both BeginInvoke and
            // EndInvoke.
            int dummy = 0;

            // Initiate the asynchronous call, passing three seconds (3000 ms)
            // for the callDuration parameter of TestMethod; a dummy variable
            // for the out parameter (threadId); the callback delegate; and
            // state information that can be retrieved by the callback method.
            // In this case, the state information is a string that can be used
            // to format a console message.

            //IAsyncResult result = caller.BeginInvoke(3000,
            //    out dummy,
            //    new AsyncCallback(CallbackMethod),
            //    "The call executed on thread {0}, with return value \"{1}\".");

            //IAsyncResult result = caller.BeginInvoke(3000, out dummy, delegate(IAsyncResult ar)
            //{
            //    //// Retrieve the delegate.
            //    //AsyncResult result = (AsyncResult)ar;
            //    //AsyncMethodCaller calle = (AsyncMethodCaller)result.AsyncDelegate;

            //    // Retrieve the format string that was passed as state
            //    // information.
            //    string formatString = (string)ar.AsyncState;

            //    // Define a variable to receive the value of the out parameter.
            //    // If the parameter were ref rather than out then it would have to
            //    // be a class-level field so it could also be passed to BeginInvoke.
            //    int threadId = 0;

            //    // Call EndInvoke to retrieve the results.
            //    string returnValue = caller.EndInvoke(out threadId, ar);

            //    // Use the format string to format the output message.
            //    Console.WriteLine(formatString, threadId, returnValue);

            //}, "The call executed on thread {0}, with return value \"{1}\".");

            IAsyncResult result = caller.BeginInvoke(3000, out dummy, (ar) =>
            {
                //// Retrieve the delegate.
                //AsyncResult result = (AsyncResult)ar;
                //AsyncMethodCaller calle = (AsyncMethodCaller)result.AsyncDelegate;

                // Retrieve the format string that was passed as state
                // information.
                string formatString = (string)ar.AsyncState;

                // Define a variable to receive the value of the out parameter.
                // If the parameter were ref rather than out then it would have to
                // be a class-level field so it could also be passed to BeginInvoke.
                int threadId = 0;

                // Call EndInvoke to retrieve the results.
                string returnValue = caller.EndInvoke(out threadId, ar);

                // Use the format string to format the output message.
                Console.WriteLine(formatString, threadId, returnValue);
            }, "The call executed on thread {0}, with return value \"{1}\".");

            // 执行50毫秒后超时
            result.AsyncWaitHandle.WaitOne(50, true);

            Console.WriteLine("The main thread {0} continues to execute...",
                              Thread.CurrentThread.ManagedThreadId);

            // The callback is made on a ThreadPool thread. ThreadPool threads
            // are background threads, which do not keep the application running
            // if the main thread ends. Comment out the next line to demonstrate
            // this.
            Thread.Sleep(4000);

            Console.WriteLine("The main thread ends.");
        }