View Full Version : .Net Redirect Standard Error xvid_encraw
Ac3Dc3
14th July 2006, 10:25
Hi-
i'm trying to redirect the standard error from xvid_encraw.exe to a .Net application written in C#. heres the code:
System.Diagnostics.Process encrawProc = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo encrawSI = new System.Diagnostics.ProcessStartInfo(encrawPath, encrawCmdLine);
encrawSI.UseShellExecute = false;
encrawSI.CreateNoWindow = true;
encrawSI.RedirectStandardError = true;
encrawProc.StartInfo = encrawSI;
encrawProc.ErrorDataReceived += new DataReceivedEventHandler(EncrawDataRecieved);
encrawProc.Start();
encrawProc.BeginErrorReadLine();
encrawProc.WaitForExit();
this works fine when the encrawCmdLine is -h but when it is anything else*, it doesnt.. i place a breakpoint at my EncrawDataRecieved handler but its like the event is never raised. is there any reason why this should be the case?
*argument thats actually work
Hmm.. Considered that this is the next step in my C# GUI (http://forum.doom9.org/showthread.php?t=112808), I'm really interested in this issue.. in the weekend I'll make some code..
Little question: did you manage to redirect the output while encoding? I'm not able to correctly redirect it with threads. PM me if seems too OT :)
Doom9
14th July 2006, 12:31
there's at least one c# gui app that uses encraw... megui
Ac3Dc3
14th July 2006, 16:19
mod: no i have not been able to do that, at least not with that code. when the arguments aren't just -h (which displays the help), but are cli options that make it encode, i cannot get it to redirect properly.. and whats the threads issue you are referring to?
doom9: yessir, and its a great app. the reason why i didnt just copy and paste the source code (for my own use) is partly because i am using the backgroundworker class instead of having to Invoke methods in other threads but mainly because, in this particular instance, i'm trying to get the most from events & delegates by staying clear of loops.
iiuc in MeGui, an XvidEncoder object uses its readStdErr method to read the standard output. it inherits this function from the CommandlineVideoEncoder class and looks something like this:
protected void readStdErr()
{
StreamReader sr = null;
try
{
sr = proc.StandardError;
}
catch (Exception e)
{
log.Append("exception getting io reader for stderr: " + e.Message + "\r\nAborting CommandlineVideoEncoder.readStdErr");
return;
}
string line;
if (proc != null)
{
try
{
while ((line = sr.ReadLine()) != null)
{
mre.WaitOne();
EncoderOutputReceived(line, 1);
}
}
catch (Exception e)
{
EncoderOutputReceived("Exception in readStdErr: " + e.Message, 2);
}
stderrDone.Set();
}
}
so it keeps reading from the standard error stream in that while loop and checking that its getting something. whereas, what im trying to do, is initiate an asyncronous read operation with BeginErrorReadLine()
whats the threads issue you are referring to?
I want to read the lines while the encoder is going, and print them in a textbox. Of course I can't wait for the encoder to finish the job.. :)
So I have to use the ProcessRunner class.
Doom9
15th July 2006, 13:19
There's another way we discussed in the dev thread of megui.. it consists of enabling events on the process, then write an event handler for the events fired when the application writes a new line to stdout/stderr - this allows you to bypass the methodinvokers I'm using. The way back to the GUI thread (if you need to show any progress) .. I don't know any other ways than the event/delegate approach coupled with an invoke (the invoke ensures the gui updates at a time the gui can handle it.. otherwise you risk deadlocks).. the alternative would be a timer in the gui that requests information at a given interval.. but imho that's wasting resources.
MeteorRain
18th July 2006, 13:52
i have read the source code of megui, and then i knew how to do and wrote the MKVYAG……
here's some code of my program:
proc = new Process();
proc.StartInfo.FileName = "mkvmerge.exe";
proc.StartInfo.Arguments = "-i \"" + filename + "\"";
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
txtlog.AppendText("Identifying File Type [" + proc.StartInfo.FileName + " " + proc.StartInfo.Arguments + "]\r\n");
proc.Start();
StreamReader sr = null;
string line, errtext = "", type;
sr = proc.StandardOutput;
if(sr != null)
{
while(null != (line = sr.ReadLine()))
{
if(line.IndexOf("Error") >= 0)
{
errtext = line;
txtlog.AppendText("Error detected: " + errtext + Environment.NewLine);
MessageBox.Show(errtext, "Error detected");
break;
}
else if(line.IndexOf("container: ") >= 0)
{
}
else if(line.StartsWith("Track"))
{
}
else if(line.StartsWith("Attachment"))
{
}
}
txtlog.AppendText(sr.ReadToEnd()); // should not happen
sr.Close();
proc.Dispose();
proc = null;
}
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.