예제 #1
0
        public void OnProduceComplete(IAsyncResult asyncResult)
        {
            //stateList.Add("执行中");
            AsyncResult     result  = (AsyncResult)asyncResult;
            ProductDelegate del     = (ProductDelegate)result.AsyncDelegate;
            string          product = del.EndInvoke(asyncResult);

            base.Complete(produce.Result, false);
            //stateList.Add("执行结束");
        }
        XInt IFactorialFunction.Factorial(int n)
        {
            if (n < 0)
            {
                throw new System.ArgumentOutOfRangeException(
                          this.Name + ": " + nameof(n) + " >= 0 required, but was " + n);
            }

            if (n < 2)
            {
                return(XInt.One);
            }

            var             log2N        = XMath.FloorLog2(n);
            ProductDelegate prodDelegate = Product;
            var             results      = new IAsyncResult[log2N];

            int high = n, low = n >> 1, shift = low, taskCounter = 0;

            // -- It is more efficient to add the big intervals
            // -- first and the small ones later!
            while ((low + 1) < high)
            {
                results[taskCounter++] = prodDelegate.BeginInvoke(low + 1, high, null, null);
                high   = low;
                low  >>= 1;
                shift += low;
            }

            XInt p = XInt.One, r = XInt.One;

            while (--taskCounter >= 0)
            {
                var I = Task.Factory.StartNew(() => r * p);
                var t = p * prodDelegate.EndInvoke(results[taskCounter]);
                r = I.Result;
                p = t;
            }

            return((r * p) << shift);
        }