我有一个WPF应用程序,它被用作触发jar文件执行的前端。我目前正在使用BackGroundWorker进行此操作,但是无法向前端报告后台执行的进度。执行可能会花费很长时间,因此向客户展示进度很重要。这是一些代码:
worker = new BackgroundWorker();
worker.DoWork += worker_DoWork;
worker.ProgressChanged += new ProgressChangedEventHandler(ProgressChanged);
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
worker.RunWorkerAsync(); //start transfer process
public void worker_DoWork(object sender, DoWorkEventArgs e)
{
Process Process = new Process();
ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + Command); //command is the jar file to be execute. It will copy files.
ProcessInfo.RedirectStandardError = true;
ProcessInfo.CreateNoWindow = false;
ProcessInfo.UseShellExecute = false;
Process = Process.Start(ProcessInfo);
Process.WaitForExit();
ExitCode = Process.ExitCode;
Process.Close();
return ExitCode;
}
我在前端有一个进度条,可以使用worker.ReportProgress()进行更新,但是我需要了解如何从backGroundWorker获取信息以将其传递给worker.ReportProgress()。或通过其他任何方式报告进度。
非常感谢!