Пример #1
0
        private async Task WriteLines(ICollection <string> argStrs)
        {
            mContext.PadProgress.Maximum += argStrs.Count;
            foreach (var s in argStrs)
            {
                mContext.CancelToken.ThrowIfCancellationRequested();
                await WriteLine(s);

                mContext.IncProgress();
            }
        }
Пример #2
0
        public async Task <EvaluateResult> Evaluate(IList <string> argExpr)
        {
            try
            {
                var pStopWatch = new Stopwatch();
                var pResult    = new EvaluateResult();

                pStopWatch.Start();
                if (argExpr == null || argExpr.Count == 0 || argExpr.All(s => string.IsNullOrWhiteSpace(s)))
                {
                    mContext.PadProgress.Name = null;
                    mContext.ReportProgress();

                    return(null);
                }

                using (var pFilesTmp = new TempFileCollection(Path.GetTempPath()))
                {
                    if (DebugEvaluate)
                    {
                        mSourceCode = pFilesTmp.AddExtension(".cs", true);
                    }

                    var pGenerate = new GenerateClassUserExpression(mContext);

                    using (var pWriterBase = (DebugEvaluate) ? (TextWriter) new StreamWriter(mSourceCode) : new StringWriter())
                    {
                        mContext.PadProgress.Maximum += 3;
                        mContext.ReportProgress();
                        await pGenerate.Generate(pWriterBase, argExpr);

                        mContext.CancelToken.ThrowIfCancellationRequested();
                        mContext.IncProgress();

                        if (!DebugEvaluate)
                        {
                            mSourceCode = pWriterBase.ToString();
                        }
                    }
                    var cp = new CompilerParameters(pGenerate.Assembles.ToArray())
                    {
                        GenerateInMemory = !DebugEvaluate
                    };

                    if (DebugEvaluate)
                    {
                        cp.IncludeDebugInformation = true;
                        cp.OutputAssembly          = Path.ChangeExtension(mSourceCode, ".dll");
                    }
                    else
                    {
                        cp.CompilerOptions = "/optimize";
                    }

                    var pProvider = Common.HelperCodeCom.CodeProvider.Value;
                    var cr        = await Task.Run(() => (DebugEvaluate)?pProvider.CompileAssemblyFromFile(cp, mSourceCode) : pProvider.CompileAssemblyFromSource(cp, mSourceCode));

                    mContext.CancelToken.ThrowIfCancellationRequested();
                    mContext.IncProgress();

                    if (cr.Errors.Count > 0)
                    {
                        mContext.PadProgress.Name = Properties.Resources.ErrorEvaluate;
                        mContext.ReportProgress();
                        if (DebugEvaluate)
                        {
                            System.Diagnostics.Process.Start(mSourceCode);
                        }
                    }
                    else
                    {
                        await Evaluate(pGenerate, cr, pResult);

                        if (!pResult.Success)
                        {
                            mContext.PadProgress.Name = Properties.Resources.ErrorEvaluate;
                        }
                        else
                        {
                            //Evaluate(cr,pr)
                            pStopWatch.Stop();
                            mContext.PadProgress.Name = string.Format(Properties.Resources.EvaluateSuccess, pStopWatch.Elapsed.Seconds);
                        }
                        mContext.PadProgress.Progress++;
                        mContext.ReportProgress();
                    }
                }

                return(pResult);
            }
            catch
            {
                mContext.PadProgress.Name = Properties.Resources.ErrorEvaluate;

                throw;
            }
            finally
            {
                mContext.PadProgress.Visible = false;
                mContext.ReportProgress();
            }
        }