C# 在c中添加progressBar以查看进程的进度百分比#

C# 在c中添加progressBar以查看进程的进度百分比#,c#,process,progress-bar,C#,Process,Progress Bar,这是关于我的流程的代码: StreamReader outputReader = null; StreamReader errorReader = null; ProcessStartInfo processStartInfo = new ProcessStartInfo(......); processStartInfo.ErrorDialog = false; //Execute the process Process proc

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

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添加到我的表单中,以计算此过程的进度百分比,但我不知道如何做


Im使用Visual Studio 2012,windows窗体。

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

如果控制该进程,则可能会将其写入标准输出或标准错误,并使用

outputReader = process.StandardOutput;
errorReader = process.StandardError;
您已定义将该进度读回程序中。例如,进程可以写入标准错误

10
31
50
99
您的父进程读取
errorReader
,可以将这些单独的行解释为完成%


一旦有了获取子流程完成百分比的方法,就可以使用来显示进度。

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

在本例中,流程的输出将设置最大值,接下来的每个步骤都会将其增大

e、 g


. 在代码中设置进度条是很容易的,知道自己实际取得了多大进展完全是另一回事。对我来说似乎很难。你能写一个清楚的例子吗?
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;
}