Пример #1
0
 protected void OnCalculatePrimeCompleted(CalculatePrimeCompletedEventArgs e)
 {
     if (CalculatePrimeCompleted != null)
     {
         CalculatePrimeCompleted(this, e);
     }
 }
Пример #2
0
        // This is the method that the underlying, free-threaded
        // asynchronous behavior will invoke.  This will happen on
        // an arbitrary thread.
        /// <summary>
        /// 实现完成方法。 它采用六个参数;
        /// 在通过客户端的 CalculatePrimeCompletedEventHandler 返回客户端的 CalculatePrimeCompletedEventArgs 中填充的就是这些参数。 它将客户端的任务 ID 标记从内部集合中移除,然后调用 PostOperationCompleted 结束异步操作的生存期。
        /// AsyncOperation 会将此调用封送到适合于应用程序模型的线程或上下文。
        /// </summary>
        /// <param name="numberToTest"></param>
        /// <param name="firstDivisor"></param>
        /// <param name="isPrime"></param>
        /// <param name="exception"></param>
        /// <param name="canceled"></param>
        /// <param name="asyncOp"></param>
        private void CompletionMethod(
            int numberToTest,
            int firstDivisor,
            bool isPrime,
            Exception exception,
            bool canceled,
            AsyncOperation asyncOp)
        {
            // If the task was not previously canceled,
            // remove the task from the lifetime collection.
            if (!canceled)
            {
                lock (userStateToLifetime.SyncRoot)
                {
                    userStateToLifetime.Remove(asyncOp.UserSuppliedState);
                }
            }

            // Package the results of the operation in a
            // CalculatePrimeCompletedEventArgs.
            CalculatePrimeCompletedEventArgs e =
                new CalculatePrimeCompletedEventArgs(
                    numberToTest,
                    firstDivisor,
                    isPrime,
                    exception,
                    canceled,
                    asyncOp.UserSuppliedState);

            // End the task. The asyncOp object is responsible
            // for marshaling the call.
            asyncOp.PostOperationCompleted(onCompletedDelegate, e);

            // Note that after the call to OperationCompleted,
            // asyncOp is no longer usable, and any attempt to use it
            // will cause an exception to be thrown.
        }
Пример #3
0
        // This event handler updates the ListView control when the
        // PrimeNumberCalculator raises the CalculatePrimeCompleted
        // event. The ListView item is updated with the appropriate
        // outcome of the calculation: Canceled, Error, or result.
        private void primeNumberCalculator1_CalculatePrimeCompleted(
            object sender,
            CalculatePrimeCompletedEventArgs e)
        {
            Guid taskId = (Guid)e.UserState;

            if (e.Cancelled)
            {
                string result = "Canceled";
                Console.WriteLine(result);
            }
            else if (e.Error != null)
            {
                string result = "Error";

                Console.WriteLine(result);
            }
            else
            {
                bool result = e.IsPrime;

                Console.WriteLine(result);
            }
        }
Пример #4
0
        // This method is invoked via the AsyncOperation object,
        // so it is guaranteed to be executed on the correct thread.
        private void CalculateCompleted(object operationState)
        {
            CalculatePrimeCompletedEventArgs e = operationState as CalculatePrimeCompletedEventArgs;

            OnCalculatePrimeCompleted(e);
        }