晚上好,
我刚刚开始使用C#,我尝试为在命令行中运行的程序创建GUI。我已经能够让它运行,但现在我被困在尝试实现进度条。
我已阅读其他帖子,但我无法找到确切的问题或了解如何将解决方案应用于我的问题。
这是我的代码(如果这非常混乱,请道歉):
private void MethodToProcess(Object sender, DoWorkEventArgs args)
{
// Set all the strings for passthrough
String USMTPath_Work = USMTPath + USMTArch;
String USMTPath_full = USMTPath_Work + @"\Scanstate.exe";
String USMTFlags_Capture = @"/c /v:13 /o /l:scanstate.log /localonly /efs:copyraw";
String Argument_full = SavePath + XML1 + XML2 + USMTFlags_Capture;
// Test that USMT path is correct
if (USMTPath == null)
{
MessageBox.Show("Error: There is no USMT Path defined.");
return;
}
// Test that Windows folder is correct when offline
/* if (Windows_Path == null)
{
MessageBox.Show("Error: There is no Windows Path to capture.");
return;
} */
// Runs the capture
System.Diagnostics.Process Scanstate = new System.Diagnostics.Process();
Scanstate.StartInfo.FileName = USMTPath_full;
Scanstate.StartInfo.Arguments = Argument_full;
Scanstate.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
Scanstate.StartInfo.WorkingDirectory = USMTPath_Work;
//Scanstate.StartInfo.UseShellExecute = false;
Scanstate.StartInfo.CreateNoWindow = true;
//Scanstate.StartInfo.RedirectStandardOutput = true;
Scanstate.Start();
Scanstate.WaitForExit();
String Str_ExitCode = Scanstate.ExitCode.ToString();
if (Scanstate.ExitCode == 1)
MessageBox.Show("Error: Data has not been captured. Please check the log files for details.");
if (Scanstate.ExitCode == 0)
MessageBox.Show("Success: Data has been captured. For more information, check log files.");
else
{
MessageBox.Show("Error: Unknown error has occurred. Please check the log files for details.");
MessageBox.Show("Error Code: " + Str_ExitCode);
}
Scanstate.Close();
}
基本上,我正在尝试运行scanstate.exe进程。现在,我正在尝试运行backgroundworker,以便能够检索进度并将其传递给进度条。
private void btnCapture_Click(object sender, EventArgs e)
{
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
progressBar1.Step = 1;
BackgroundWorker CaptureBG = new BackgroundWorker();
CaptureBG.WorkerReportsProgress = true;
CaptureBG.DoWork += new DoWorkEventHandler(MethodToProcess);
CaptureBG.RunWorkerCompleted +=new RunWorkerCompletedEventHandler(CaptureBG_RunWorkerCompleted);
CaptureBG.ProgressChanged += new ProgressChangedEventHandler(CaptureBG_ProgressChanged);
CaptureBG.RunWorkerAsync();
}
和
private void CaptureBG_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs args)
{
progressBar1.Value = 100;
}
private void CaptureBG_ProgressChanged(object sender, ProgressChangedEventArgs args)
{
progressBar1.Value++;
}
然而,我要么误解了使用,要么我错过了一些东西,因为这个过程会运行,但我没有在进度条上取得任何进展。只有在流程结束后才会填写。
我做错了什么?一般来说,如果我不知道要花多长时间,过程如何报告进展?
提前致谢
答案 0 :(得分:0)
BackgroundWorker 负责在进一步完成任务时更新进度。
您启动的流程与代码之间没有任何互动,这些代码会将该流程的进度提交回您的代码。
为了实现这一点,必须做两件事:
第一步是棘手的,取决于 scanstate.exe 的工作原理。它是否可以指示进度,例如写入控制台?如果是这样,您可以重定向控制台输出并解析该输出以确定或至少估计进度。
<强>更新强>
Scanstate.exe提供了将进度写入日志的功能,例如:
scanstate /i:migapp.xml /i:miguser.xml \\fileserver\migration\mystore /progress:prog.log /l:scanlog.log
您可以在BackgroundWorker中使用FileWatcher来查找进度日志的更改并相应地更新进度。