0

晚上好,

我刚开始玩 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++;
    }

但是,由于进程运行,我要么误解了使用,要么遗漏了一些东西,但我在进度条上没有任何进展。它仅在过程完成后填充。

我究竟做错了什么?一般来说,如果我不确切知道需要多长时间,流程将如何报告进度?

提前致谢

4

1 回答 1

0

BackgroundWorker负责在其任务进一步完成时更新进度。

您启动的流程与您的代码之间没有交互,该代码会将流程的进度提供给您的代码。

为了使它起作用,必须发生两件事:

  1. 您需要为您的流程定义一种机制来向BackgroundWorker报告进度。
  2. BackgroundWorker必须通过调用ReportProgress方法来更新自己的进度,以便触发ProgressChanged 事件

第一步很棘手,取决于scanstate.exe 的工作方式。它是否可以指示进度,例如写入控制台?如果是这样,您可以重定向控制台输出并解析该输出以确定或至少估计进度。

更新

Scanstate.exe提供了将进度写入日志的能力,例如:

scanstate /i:migapp.xml /i:miguser.xml \\fileserver\migration\mystore /progress:prog.log /l:scanlog.log

您可以在 BackgroundWorker 中使用FileWatcher来查找对进度日志的更改并相应地更新进度。

于 2012-08-07T03:46:16.710 回答