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); }
public Model_Invoke_Result(Invoke_Produce produce, AsyncCallback callback, object state) : base(callback, state) { this.produce = produce; //this.State=new List<string>(); //stateList = this.State as List<string>; //Turn the expression into an array of bytes produce.RunWork(); //string sta1 = "开始执行"; //stateList.Add(sta1); ProductDelegate del = new ProductDelegate(produce.Product); AsyncCallback callBack = new AsyncCallback(OnProduceComplete); //string currentState = "kaishi"; IAsyncResult asyncResult = del.BeginInvoke(callBack, this); if (!produce.Finish) { return; } base.Complete(produce.Result, true); //begin writing asynchronously //IAsyncResult result = fs.BeginWrite(bytes, 0, bytes.Length, new AsyncCallback(OnWrite), thi }