Пример #1
0
        private IEnumerator RefinementProcessing(CalculationManager manager, Conditions conditions, AeroPredictor vessel, EnvelopePoint[,] basisData, Conditions basisConditions = new Conditions(), Queue <Conditions> followOnConditions = null, bool forcePushToGraph = false)
        {
            int numPtsX = (int)Math.Ceiling((conditions.upperBoundSpeed - conditions.lowerBoundSpeed) / conditions.stepSpeed);
            int numPtsY = (int)Math.Ceiling((conditions.upperBoundAltitude - conditions.lowerBoundAltitude) / conditions.stepAltitude);

            EnvelopePoint[,] newEnvelopePoints = new EnvelopePoint[numPtsX + 1, numPtsY + 1];

            CalculationManager backgroundManager = new CalculationManager();

            manager.OnCancelCallback           += backgroundManager.Cancel;
            CalculationManager.State[,] results = new CalculationManager.State[numPtsX + 1, numPtsY + 1];
            GenData rootData = new GenData(vessel, conditions, 0, 0, backgroundManager);

            ThreadPool.QueueUserWorkItem(ContinueInBackground, new object[] { rootData, results, basisData, basisConditions });
            while (!backgroundManager.Completed)
            {
                if (manager.Status == CalculationManager.RunStatus.Cancelled)
                {
                    backgroundManager.Cancel();
                    yield break;
                }
                yield return(0);
            }
            manager.OnCancelCallback -= backgroundManager.Cancel;

            newEnvelopePoints = ((CalculationManager.State[, ])rootData.storeState.Result)
                                .SelectToArray(pt => (EnvelopePoint)pt.Result);

            AddToCache(conditions, newEnvelopePoints);
            if (currentConditions.Equals(conditions) || (forcePushToGraph && !backgroundManager.Cancelled))
            {
                envelopePoints    = newEnvelopePoints;
                currentConditions = conditions;
                UpdateGraphs();
                valuesSet = true;
            }
            backgroundManager.Dispose();
            if (!manager.Cancelled && followOnConditions != null && followOnConditions.Count > 0)
            {
                yield return(0);

                Conditions nextConditions = followOnConditions.Dequeue();
                WindTunnel.Instance.StartCoroutine(RefinementProcessing(manager, nextConditions, vessel, newEnvelopePoints, conditions, followOnConditions, forcePushToGraph));
            }
        }
 public virtual void Cancel()
 {
     calculationManager.Cancel();
     calculationManager = new CalculationManager();
     valuesSet          = false;
 }
        private IEnumerator Processing(CalculationManager manager, Conditions conditions, AeroPredictor vessel)
        {
            int numPtsX = (int)Math.Ceiling((conditions.upperBoundSpeed - conditions.lowerBoundSpeed) / conditions.stepSpeed);
            int numPtsY = (int)Math.Ceiling((conditions.upperBoundAltitude - conditions.lowerBoundAltitude) / conditions.stepAltitude);

            EnvelopePoint[,] newEnvelopePoints = new EnvelopePoint[numPtsX + 1, numPtsY + 1];

            GenData rootData = new GenData(vessel, conditions, 0, 0, manager);

            ThreadPool.QueueUserWorkItem(SetupInBackground, rootData);

            while (!manager.Completed)
            {
                //Debug.Log(manager.PercentComplete + "% done calculating...");
                if (manager.Status == CalculationManager.RunStatus.Cancelled)
                {
                    yield break;
                }
                yield return(0);
            }

            newEnvelopePoints = ((CalculationManager.State[, ])rootData.storeState.Result)
                                .SelectToArray(pt => (EnvelopePoint)pt.Result);

            if (!manager.Cancelled)
            {
                //cache.Add(conditions, newEnvelopePoints);
                AddToCache(conditions, newEnvelopePoints);
                envelopePoints    = newEnvelopePoints;
                currentConditions = conditions;
                GenerateGraphs();
                valuesSet = true;
            }

            float stepSpeed = conditions.stepSpeed, stepAltitude = conditions.stepAltitude;

            for (int i = 2; i <= 2; i++)
            {
                yield return(0);

                CalculationManager backgroundManager = new CalculationManager();
                manager.OnCancelCallback += backgroundManager.Cancel;
                conditions = new Conditions(conditions.body, conditions.lowerBoundSpeed, conditions.upperBoundSpeed,
                                            stepSpeed / i, conditions.lowerBoundAltitude, conditions.upperBoundAltitude, stepAltitude / i);
                CalculationManager.State[,] prevResults = ((CalculationManager.State[, ])rootData.storeState.Result).SelectToArray(p => p);
                rootData = new GenData(vessel, conditions, 0, 0, backgroundManager);
                ThreadPool.QueueUserWorkItem(ContinueInBackground, new object[] { rootData, prevResults });
                while (!backgroundManager.Completed)
                {
                    if (manager.Status == CalculationManager.RunStatus.Cancelled)
                    {
                        backgroundManager.Cancel();
                        yield break;
                    }
                    yield return(0);
                }

                newEnvelopePoints = ((CalculationManager.State[, ])rootData.storeState.Result)
                                    .SelectToArray(pt => (EnvelopePoint)pt.Result);

                if (!manager.Cancelled)
                {
                    //cache.Add(conditions, newEnvelopePoints);
                    AddToCache(conditions, newEnvelopePoints);
                    envelopePoints    = newEnvelopePoints;
                    currentConditions = conditions;
                    UpdateGraphs();
                    valuesSet = true;
                }
            }
        }