private IEnumerator Processing(CalculationManager manager, Conditions conditions, AeroPredictor vessel) { int numPts = (int)Math.Ceiling((conditions.upperBound - conditions.lowerBound) / conditions.step); VelPoint[] newVelPoints = new VelPoint[numPts + 1]; float trueStep = (conditions.upperBound - conditions.lowerBound) / numPts; CalculationManager.State[] results = new CalculationManager.State[numPts + 1]; for (int i = 0; i <= numPts; i++) { //newAoAPoints[i] = new AoAPoint(vessel, conditions.body, conditions.altitude, conditions.speed, conditions.lowerBound + trueStep * i); GenData genData = new GenData(vessel, conditions, conditions.lowerBound + trueStep * i, manager); results[i] = genData.storeState; ThreadPool.QueueUserWorkItem(GenerateVelPoint, genData); } while (!manager.Completed) { if (manager.Status == CalculationManager.RunStatus.Cancelled) { yield break; } yield return(0); } for (int i = 0; i <= numPts; i++) { newVelPoints[i] = (VelPoint)results[i].Result; } if (!manager.Cancelled) { cache.Add(conditions, newVelPoints); VelPoints = newVelPoints; currentConditions = conditions; UpdateGraphs(); valuesSet = true; } }
private IEnumerator Processing(Conditions conditions) { int numPts = (int)Math.Ceiling((conditions.upperBound - conditions.lowerBound) / conditions.step); primaryProgress = new VelPoint[numPts + 1]; float trueStep = (conditions.upperBound - conditions.lowerBound) / numPts; CancellationTokenSource closureCancellationTokenSource = this.cancellationTokenSource; stopwatch.Reset(); stopwatch.Start(); task = Task.Factory.StartNew <VelPoint[]>( () => { try { //OrderablePartitioner<EnvelopePoint> partitioner = Partitioner.Create(primaryProgress, true); Parallel.For <AeroPredictor>(0, primaryProgress.Length, new ParallelOptions() { CancellationToken = closureCancellationTokenSource.Token }, WindTunnelWindow.Instance.GetAeroPredictor, (index, state, predictor) => { primaryProgress[index] = new VelPoint(predictor, conditions.body, conditions.altitude, conditions.lowerBound + trueStep * index); return(predictor); }, (predictor) => (predictor as VesselCache.IReleasable)?.Release()); closureCancellationTokenSource.Token.ThrowIfCancellationRequested(); return(cache[conditions] = primaryProgress); } catch (AggregateException aggregateException) { foreach (var ex in aggregateException.Flatten().InnerExceptions) { Debug.LogException(ex); } throw aggregateException; } }, closureCancellationTokenSource.Token); while (task.Status < TaskStatus.RanToCompletion) { //Debug.Log(manager.PercentComplete + "% done calculating..."); yield return(0); } if (task.Status > TaskStatus.RanToCompletion) { if (task.Status == TaskStatus.Faulted) { Debug.LogError("Wind tunnel task faulted (Vel)"); Debug.LogException(task.Exception); } else if (task.Status == TaskStatus.Canceled) { Debug.Log("Wind tunnel task was canceled. (Vel)"); } yield break; } if (!closureCancellationTokenSource.IsCancellationRequested) { VelPoints = primaryProgress; currentConditions = conditions; UpdateGraphs(); valuesSet = true; } }