1

这是关于我的过程的代码:

StreamReader outputReader = null;
StreamReader errorReader = null;


       ProcessStartInfo processStartInfo = new ProcessStartInfo(......);
       processStartInfo.ErrorDialog = false;

       //Execute the process
        Process process = new Process();
        process.StartInfo = processStartInfo;
        bool processStarted = process.Start();

                     if (processStarted)
                        {
                        //Get the output stream
                        outputReader = process.StandardOutput;
                        errorReader = process.StandardError;


                        //Display the result
                        string displayText = "Output" + Environment.NewLine + "==============" + Environment.NewLine;
                        displayText += outputReader.ReadToEnd();
                        displayText += Environment.NewLine + Environment.NewLine + "==============" +
                                       Environment.NewLine;
                        displayText += errorReader.ReadToEnd();
                        // txtResult.Text = displayText;
                    }

我需要将 progressBar 添加到我的表单中以计算此过程的进度百分比,但我不知道该怎么做。

我使用 Visual Studio 2012,windows 窗体。

4

2 回答 2

3

使用流程OutputDataReceived事件来捕获进度。(假设该过程正在提供任何类型的更新)。您可以格式化初始输出以返回增量总数,然后为每个输出事件增加进度或实际解析输出数据以确定当前进度。

在此示例中,该过程的输出将设置最大值,随后的每个步骤都会将其提高。

例如

progressBar1.Style = ProgressBarStyle.Continuous;
// for every line written to stdOut, raise a progress event
int result = SpawnProcessSynchronous(fileName, args, out placeholder, false,
    (sender, eventArgs) =>
    {
        if (eventArgs.Data.StartsWith("TotalSteps=")
        {
          progressBar1.Minimum = 0;
          progressBar1.Maximum = Convert.ToInt32(eventArgs.Data.Replace("TotalSteps=",""));
          progressBar1.Value = 0;
        }
        else
        {
          progressBar1.Increment(1);
        }
    });


public static int SpawnProcessSynchronous(string fileName, string args, out string stdOut, bool isVisible, DataReceivedEventHandler OutputDataReceivedDelegate)
{
    int returnValue = 0;
    var processInfo = new ProcessStartInfo();
    stdOut = "";
    processInfo.FileName = fileName;
    processInfo.WorkingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? "";
    log.Debug("Set working directory to: {0}", processInfo.WorkingDirectory);

    processInfo.WindowStyle = isVisible ? ProcessWindowStyle.Normal : ProcessWindowStyle.Hidden;
    processInfo.UseShellExecute = false;
    processInfo.RedirectStandardOutput = true;
    processInfo.CreateNoWindow = true;

    processInfo.Arguments = args;
    using (Process process = Process.Start(processInfo))
    {
        if (OutputDataReceivedDelegate != null)
        {
            process.OutputDataReceived += OutputDataReceivedDelegate;
            process.BeginOutputReadLine();
        }
        else
        {
            stdOut = process.StandardOutput.ReadToEnd();
        }
        // do not reverse order of synchronous read to end and WaitForExit or deadlock
        // Wait for the process to end.  
        process.WaitForExit();
        returnValue = process.ExitCode;
    }
    return returnValue;
}
于 2013-02-13T17:37:16.510 回答
0

通用流程没有提供进度通知的内置机制。您需要为您开始通知其进度的过程找出一些方法。

如果您控制该过程,您可以让它写入标准输出或标准错误,并使用

outputReader = process.StandardOutput;
errorReader = process.StandardError;

您已定义将该进度读回您的程序中。例如,该进程可以写入标准错误

10
31
50
99

并且您的父进程 readingerrorReader可以将这些单独的行解释为 % complete。

一旦您有办法获得子进程的完成百分比,您就可以使用ProgressBar来显示该进度。

于 2013-02-12T21:57:43.863 回答