Exemple #1
0
        //allows derived classes to override the default streaming
        //and save method
        protected virtual async Task <bool> StreamAndSaveCalculationAsync()
        {
            bool bHasCalculations = false;

            //both calculators and analyzers use observationpath for initial file
            if (!CalculatorHelpers.URIAbsoluteExists(this.GCCalculatorParams.ExtensionDocToCalcURI,
                                                     this.GCCalculatorParams.AnalyzerParms.ObservationsPath))
            {
                this.GCCalculatorParams.AnalyzerParms.ObservationsPath
                    = this.GCCalculatorParams.ExtensionDocToCalcURI.URIDataManager.TempDocPath;
            }
            if (!CalculatorHelpers.URIAbsoluteExists(this.GCCalculatorParams.ExtensionDocToCalcURI,
                                                     this.GCCalculatorParams.AnalyzerParms.ObservationsPath))
            {
                return(bHasCalculations);
            }
            //new temporary path to store calculator results
            StringWriter output = new StringWriter();

            this.GCCalculatorParams.DocToCalcReader = await CalculatorHelpers.GetXmlReaderAsync(
                this.GCCalculatorParams.ExtensionDocToCalcURI, this.GCCalculatorParams.AnalyzerParms.ObservationsPath);

            if (this.GCCalculatorParams.DocToCalcReader != null)
            {
                using (this.GCCalculatorParams.DocToCalcReader)
                {
                    XmlWriterSettings writerSettings = new XmlWriterSettings();
                    writerSettings.OmitXmlDeclaration = true;
                    writerSettings.Indent             = true;
                    //writerSettings.Async = true;
                    using (XmlWriter writer = XmlWriter.Create(
                               output, writerSettings))
                    {
                        XStreamingElement root = new XStreamingElement(Constants.ROOT_PATH,
                                                                       from el in StreamRoot()
                                                                       select el);
                        root.Save(writer);
                        //alternative pattern
                        ////now process each task as it writes
                        //bool[] hasCalcs = await Task.WhenAll(_lvTasks);
                        //for (int i = 0; i < hasCalcs.Count(); i++)
                        //{
                        //    bool bHasCalc = hasCalcs[i];
                        //}
                    }
                }
            }
            using (output)
            {
                if (this.GCCalculatorParams.ErrorMessage == string.Empty)
                {
                    //move the new calculations to tempDocToCalcPath
                    //this returns an error msg
                    this.GCCalculatorParams.ErrorMessage = await CalculatorHelpers.MoveURIsAsync(
                        this.GCCalculatorParams.ExtensionDocToCalcURI, output,
                        this.GCCalculatorParams.ExtensionDocToCalcURI.URIDataManager.TempDocPath);

                    bHasCalculations = true;
                }
            }
            return(bHasCalculations);
        }
        //async calcs to complete
        //List<Task<bool>> _lvTasks = new List<Task<bool>>();
        //allows derived classes to override the default streaming
        //and save method
        protected virtual async Task <bool> StreamAndSaveCalculationAsync()
        {
            bool bHasCalculations = false;

            //both calculators and analyzers use observationpath for initial file
            if (!CalculatorHelpers.URIAbsoluteExists(this.GCCalculatorParams.ExtensionDocToCalcURI,
                                                     this.GCCalculatorParams.AnalyzerParms.ObservationsPath))
            {
                this.GCCalculatorParams.AnalyzerParms.ObservationsPath
                    = this.GCCalculatorParams.ExtensionDocToCalcURI.URIDataManager.TempDocPath;
            }
            if (!CalculatorHelpers.URIAbsoluteExists(this.GCCalculatorParams.ExtensionDocToCalcURI,
                                                     this.GCCalculatorParams.AnalyzerParms.ObservationsPath))
            {
                return(bHasCalculations);
            }
            //new temporary path to store calculator results
            //when writing to file, close it with the using
            StringWriter output = new StringWriter();

            this.GCCalculatorParams.DocToCalcReader = await CalculatorHelpers.GetXmlReaderAsync(
                this.GCCalculatorParams.ExtensionDocToCalcURI, this.GCCalculatorParams.AnalyzerParms.ObservationsPath);

            if (this.GCCalculatorParams.DocToCalcReader != null)
            {
                using (this.GCCalculatorParams.DocToCalcReader)
                {
                    XmlWriterSettings writerSettings = new XmlWriterSettings();
                    writerSettings.OmitXmlDeclaration = true;
                    writerSettings.Indent             = true;
                    ////can now write this this asynch
                    //writerSettings.Async = true;
                    using (XmlWriter writer = XmlWriter.Create(
                               output, writerSettings))
                    {
                        //180: switched to XStreaming, uses less memory until xml is written
                        XStreamingElement root = new XStreamingElement(Constants.ROOT_PATH,
                                                                       from el in StreamingRoot()
                                                                       select el);
                        //the write will omit xml declaration (allowing xslt transform)
                        //the write will also cause the calctasks to be completed
                        root.Save(writer);

                        //keep this code to verify that tasks are being completed once xml is serialized
                        //test whether or not this interferes with sequential completion of tasks for aggregation
                        ////now process each task as it writes
                        //bool[] hasCalcs = await Task.WhenAll(_lvTasks);
                        //for (int i = 0; i < hasCalcs.Count(); i++)
                        //{
                        //    bool bHasCalc = hasCalcs[i];
                        //}
                    }
                }
            }
            //subsequent async method calls have to use Wait()
            //or the async returns here without the completed calculations
            //see SB1GeneralCalculatorAsync.RunCalculationsAndSetUpdatesAsync
            using (output)
            {
                if (this.GCCalculatorParams.ErrorMessage == string.Empty)
                {
                    //move the new calculations to tempDocToCalcPath
                    //this returns an error msg
                    this.GCCalculatorParams.ErrorMessage = await CalculatorHelpers.MoveURIsAsync(
                        this.GCCalculatorParams.ExtensionDocToCalcURI, output,
                        this.GCCalculatorParams.ExtensionDocToCalcURI.URIDataManager.TempDocPath);

                    bHasCalculations = true;
                }
            }
            return(bHasCalculations);
        }