值“重置";任务完成后返回零C# [英] Values "resetting" back to zero after Task is completed C#

查看:138
本文介绍了值“重置";任务完成后返回零C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的函数,它返回一个结构,其中包含成功命令的数量,有错误的命令的数量以及总数 两者以及它们之间的文件路径都将在Iprogress接口中执行.

This is my function, it returns a struct with the number of successful commands, the number of the ones with errors and also the total of both and also the file path from wich they are being exectued in a Iprogress interface.

   public void ExecuteCommands(string Directories, CdpsiUpdateSql Updater, CdpsiUpdateSqlparser parser, string Log, IProgress<Result> progress)
        {
            Result FinalResult = new Result();
            int totalWithErrors = 0;
            int totalSuccess = 0;
            string[] numArray1 = new string[3];
            List<string> list = ((IEnumerable<string>)Directory.GetFiles(Directories, "*.sql", SearchOption.TopDirectoryOnly)).Select(f =>
            {
                string[] strArray = Path.GetFileName(f).Split('_');
                int result;
                if (strArray.Length < 1 || !int.TryParse(strArray[0], out result))
                    result = -1;
                var data = new
                {
                    File = f,
                    Version = result
                };
                return data;
            }).Where(f => f.Version > -1).OrderBy(f => f.Version).Select(f => f.File).ToList<string>();
            foreach (string str in list)
            {
                int[] numArray2 = this.ExecuteCommand(parser.Parser(str), Updater, str, Log);
                int Succcesses = numArray2[0];
                int Errors = numArray2[1];
                //totalWithErrors += Errors;
                //totalSuccess += Succcesses;

                FinalResult.File = str;
                FinalResult.Errors = Errors;
                FinalResult.Successes = Errors;
                //FinalResult.TotalWithErrors = totalWithErrors;
                //FinalResult.totalSuccess = totalSuccess;


                progress.Report(FinalResult);
            }
        }

在我的主代码中,我这样调用该函数:

In my main code I call the function like this:

foreach (var ConnectionString in ConnectionStrings)
        {
            connection = new OracleConnection(ConnectionString);
            using (CdpsiUpdateSql NewUpdater = new CdpsiUpdateSql(connection))
            {
                labelBDver.Text = Global.VersaoBd = NewUpdater.LookUp("GGDSC", "APPLCONFIG", "GGCOD = 'APP_NAME'");
                lbVersao.Text = "Versão: " + Global.NovaVersao;
                CpdisUpdateScripts Scripts = new CpdisUpdateScripts();
                CdpsiUpdateSqlparser Parser = new CdpsiUpdateSqlparser();
                var progressHandler = new Progress<CpdisUpdateScripts.Result>(result =>
                {
                    textBox1.Text = result.File;
                    //Global.TotalExecCerros = result.TotalWithErrors;
                    //Global.TotalExecSuccessfull = result.totalSuccess;
                    Global.TotalExecCerros += result.Errors;
                    Global.TotalExecSuccessfull += result.Successes;
                    labelErrorCommandCnt.Text = result.Errors.ToString();
                    labelSuccessfulCnt.Text = result.Successes.ToString();
                    Refresh();

                });
                IProgress<CpdisUpdateScripts.Result> progress = progressHandler;
                Task MyTask = Task.Run(() =>
                {

                    Scripts.ExecuteCommands(Global.Update + @"\scripts", NewUpdater, Parser,
                        Global.Update + @"\scripts", progress);

                });

            }
        }

在处理程序上,传递给标签的值正确,标签接收正确的值,并且Global.Cerros和Global.TotalExecSuccessfull = result.totalSuccess;.也可以接收正确数量的错误,但是当foreach完成时,两者均为0. 我不明白为什么会这样.

On the handler, the values that are passed to the labels are correct, the labels recive the correct values and the Global.Cerros and Global.TotalExecSuccessfull = result.totalSuccess; also recieve the correct number of errors, but when the foreach finishes both are 0. I can't understand why this is happening.

推荐答案

您的问题出在您的IProgress.Report(FinalResult)上.将Result对象传递给IProgress.Report方法时,您每次都需要为其创建一个新副本.

Your problem is with your IProgress.Report(FinalResult). You need to create a new copy of the Result object everytime when passing it to IProgress.Report method.

因此,要解决您的问题,您需要按以下步骤更改代码:

So to solve your problem, you need to change you code as follows:

public void ExecuteCommands(string Directories, CdpsiUpdateSql Updater, CdpsiUpdateSqlparser parser, string Log, IProgress<Result> progress)
{
    //Remove this line
    //Result FinalResult = new Result();

    //... your code as is... bla bla bla

    foreach (string str in list)
    {
        int[] numArray2 = this.ExecuteCommand(parser.Parser(str), Updater, str, Log);
        int Succcesses = numArray2[0];
        int Errors = numArray2[1];

        //new line
        Result FinalResult = new Result();
        FinalResult.File = str;
        FinalResult.Errors = Errors;
        FinalResult.Successes = Errors;

        progress.Report(FinalResult);

    }
}

这篇关于值“重置";任务完成后返回零C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆