示例#1
0
        // The following method demonstrates the asynchronous pattern
        // using a BeginInvoke, followed by waiting with a time-out.
        public void FactorizeNumberAndWait()
        {
            AsyncFactorCaller factorDelegate = new AsyncFactorCaller(PrimeFactorFinder.Factorize);

            int number = 1000589023;
            int temp   = 0;

            // Asynchronously invoke the Factorize method.
            IAsyncResult result = factorDelegate.BeginInvoke(
                number,
                ref temp,
                ref temp,
                null,
                null);

            while (!result.IsCompleted)
            {
                // Do any work you can do before waiting.
                result.AsyncWaitHandle.WaitOne(10000, false);
            }
            result.AsyncWaitHandle.Close();

            // The asynchronous operation has completed.
            int factor1 = 0;
            int factor2 = 0;

            // Obtain the result.
            bool answer = factorDelegate.EndInvoke(ref factor1, ref factor2, result);

            // Output the results.
            Console.WriteLine("Sequential : Factors of {0} : {1} {2} - {3}",
                              number, factor1, factor2, answer);
        }
示例#2
0
        // The following method demonstrates the asynchronous pattern using a callback method.
        public void FactorizeNumberUsingCallback()
        {
            AsyncFactorCaller factorDelegate = new AsyncFactorCaller(PrimeFactorFinder.Factorize);
            int number = 1000589023;
            int temp   = 0;

            // Waiter will keep the main application thread from
            // ending before the callback completes because
            // the main thread blocks until the waiter is signaled
            // in the callback.
            waiter = new ManualResetEvent(false);

            // Define the AsyncCallback delegate.
            AsyncCallback callBack = new AsyncCallback(this.FactorizedResults);

            // Asynchronously invoke the Factorize method.
            IAsyncResult result = factorDelegate.BeginInvoke(
                number,
                ref temp,
                ref temp,
                callBack,
                number);

            // Do some other useful work while
            // waiting for the asynchronous operation to complete.

            // When no more work can be done, wait.
            waiter.WaitOne();
        }
示例#3
0
        // The following method demonstrates the asynchronous pattern using a callback method. 
        public void FactorizeNumberUsingCallback()
        {
            AsyncFactorCaller factorDelegate = new AsyncFactorCaller(PrimeFactorFinder.Factorize);
            int number = 1000589023;
            int temp = 0;
            // Waiter will keep the main application thread from  
            // ending before the callback completes because 
            // the main thread blocks until the waiter is signaled 
            // in the callback.
            waiter = new ManualResetEvent(false);

            // Define the AsyncCallback delegate.
            AsyncCallback callBack = new AsyncCallback(this.FactorizedResults);

            // Asynchronously invoke the Factorize method.
            IAsyncResult result = factorDelegate.BeginInvoke(
                                 number,
                                 ref temp,
                                 ref temp,
                                 callBack,
                                 number);

            // Do some other useful work while  
            // waiting for the asynchronous operation to complete. 

            // When no more work can be done, wait.
            waiter.WaitOne();
        }
示例#4
0
        public void FactorizedResults(IAsyncResult result)
        {
            int factor1 = 0;
            int factor2 = 0;
            AsyncFactorCaller factorDelegate = ((AsyncResult)result).AsyncDelegate as AsyncFactorCaller;

            int  number = (int)result.AsyncState;
            bool answer = factorDelegate.EndInvoke(ref factor1, ref factor2, result);

            Console.WriteLine("On CallBack factors of {0}:{1}{2}{3}", number, factor1, factor2, answer);
            waiter.Set();
        }
示例#5
0
        // Define the method that receives a callback when the results are available.
        public void FactorizedResults(IAsyncResult result)
        {
            string key = "dad";

            // Extract the delegate from the
            // System.Runtime.Remoting.Messaging.AsyncResult.
            AsyncFactorCaller factorDelegate = (AsyncFactorCaller)((AsyncResult)result).AsyncDelegate;
            int number = (int)result.AsyncState;

            // Obtain the result.
            bool answer = factorDelegate.EndInvoke(ref key, result);

            waiter.Set();
        }
        // Define the method that receives a callback when the results are available.
        public void FactorizedResults(IAsyncResult result)
        {
            int factor1 = 0;
            int factor2 = 0;

            // Extract the delegate from the
            // System.Runtime.Remoting.Messaging.AsyncResult.
            AsyncFactorCaller factorDelegate = (AsyncFactorCaller)((AsyncResult)result).AsyncDelegate;
            int number = (int)result.AsyncState;
            // Obtain the result.
            bool answer = factorDelegate.EndInvoke(ref factor1, ref factor2, result);

            // Output the results.
            Console.WriteLine("On CallBack: Factors of {0} : {1} {2} - {3}", number, factor1, factor2, answer);
            waiter.Set();
        }
示例#7
0
        public void FactorizeNumberUsingCallback()
        {
            AsyncFactorCaller factorDelegate = new AsyncFactorCaller(PrimeFactorFinder.Factorize);
            int number = 1000589023;
            int temp   = 0;

            waiter = new ManualResetEvent(false);

            AsyncCallback callBack = new AsyncCallback(this.FactorizedResults);

            IAsyncResult result = factorDelegate.BeginInvoke(
                number,
                ref temp,
                ref temp,
                callBack,
                number);

            waiter.WaitOne();
        }
示例#8
0
        public void FactorizeNumberAndWait()
        {
            AsyncFactorCaller factorDelegate = new AsyncFactorCaller(PrimeFactorFinder.Factorize);

            int number = 1000589023;
            int temp   = 0;

            //开始调用

            IAsyncResult result = factorDelegate.BeginInvoke(number, ref temp, ref temp, null, null);

            while (!result.IsCompleted)
            {
                result.AsyncWaitHandle.WaitOne(10000, false);
            }
            result.AsyncWaitHandle.Close();

            int  factor1 = 0;
            int  factor2 = 0;
            bool answer  = factorDelegate.EndInvoke(ref factor1, ref factor2, result);

            Console.WriteLine("Squential;Factors of{0}:{1}{2}-{3}", number, factor1, factor2, answer);
        }
示例#9
0
        // The following method demonstrates the asynchronous pattern  
        // using a BeginInvoke, followed by waiting with a time-out. 
        public void FactorizeNumberAndWait()
        {
            AsyncFactorCaller factorDelegate = new AsyncFactorCaller(PrimeFactorFinder.Factorize);

            int number = 1000589023;
            int temp = 0;

            // Asynchronously invoke the Factorize method.
            IAsyncResult result = factorDelegate.BeginInvoke(
                              number,
                              ref temp,
                              ref temp,
                              null,
                              null);

            while (!result.IsCompleted)
            {
                // Do any work you can do before waiting.
                result.AsyncWaitHandle.WaitOne(10000, false);
            }
            result.AsyncWaitHandle.Close();

            // The asynchronous operation has completed. 
            int factor1 = 0;
            int factor2 = 0;

            // Obtain the result. 
            bool answer = factorDelegate.EndInvoke(ref factor1, ref factor2, result);

            // Output the results.
            Console.WriteLine("Sequential : Factors of {0} : {1} {2} - {3}",
                              number, factor1, factor2, answer);
        }