This is code about my process:
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;
}
I need add progressBar to my form to calculate progress percentage to this process, but I dont know how do.
Im using Visual Studio 2012, windows form.
A generic Process does not have a built-in mechanism to provide progress notification. You need to figure out some means for the process you are starting to inform of its progress.
If you control that process, you might have it write to Standard Out or Standard Error, and use the
you have defined to read that progress back into your program. For example, the process could write to Standard Error
and your parent process, reading
errorReader
, could interpret those individual lines as % complete.Once you have a means of getting the % complete of the child process, you can use a ProgressBar to show that progress.
Use the process
OutputDataReceived
event to capture progress. (assuming the process is giving any sort of updates). You could format an initial output to return the total number of increments, and then bump the progress for each output event or actually parse the output data to determine current progress.In this example the output from the process will set the maximum, and each following step will bump it up.
e.g.