问题描述
这是我的函数,它返回一个结构,其中包含成功命令的数量,有错误的命令的数量以及总数两者以及它们之间的文件路径都将在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 totalof 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);
});
}
}
推荐答案
您的问题出在您的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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!