This code executes "handbrakecli" (a command line application) and places the output into a string:
Dim p As Process = New Process
p.StartInfo.FileName = "handbrakecli"
p.StartInfo.Arguments = "-i [source] -o [destination]"
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.Start
Dim output As String = p.StandardOutput.ReadToEnd
p.WaitForExit
The problem is that this can take up to 20 minutes to complete during which nothing will be reported back to the user. Once it's completed, they'll see all the output from the application which includes progress details. Not very useful.
Therefore I'm trying to 开发者_JAVA百科find a sample that shows the best way to:
- Start an external application (hidden)
- Monitor its output periodically as it displays information about it's progress (so I can extract this and present a nice percentage bar to the user)
- Determine when the external application has finished (so I can't continue with my own applications execution)
- Kill the external application if necessary and detect when this has happened (so that if the user hits "cancel", I get take the appropriate steps)
Does anyone have any recommended code snippets?
The StandardOutput property is of type StreamReader, which has methods other than ReadToEnd. It would be more code, but if you used the Read method, you could do other things like provide the user with the opportunity to cancel or report some type of progress.
Link to Read Method with code sample:
http://msdn.microsoft.com/en-us/library/ath1fht8(v=VS.90).aspx
Edit:
The Process class also has a BeginOutputReadLine method which is an asynchronous method call with callback.
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline(v=VS.90).aspx
精彩评论