i’m trying to write a simple software who play karaoke in Mono Gtk / .NET.
I simply want to run via shell something like “timidity filename.kar” (timidity is a software synth).
I want to write timidity output into a textview, but the problem is that if i run this code, timidity start to ‘play’ but the app exits
protected virtual void OnBtnPlayClicked (object sender, System.EventArgs e)
{
string parms = filechooser.Filename;
string output = “”;
string error = string.Empty;
ProcessStartInfo psi = new ProcessStartInfo("timidity", parms);
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
psi.UseShellExecute = false;
System.Diagnostics.Process reg;
reg = System.Diagnostics.Process.Start(psi);
reg.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
reg.BeginOutputReadLine(); // HERE THE APP GO CRASH
}
private void SortOutputHandler(object sendingProcess,
DataReceivedEventArgs outLine)
{
// Collect the sort command output.
if (!String.IsNullOrEmpty(outLine.Data))
{
txtOutput.Buffer.Text =txtOutput.Buffer.Text + outLine.Data;
}
}
Where i’m wrong ?
Thanks
You are attempting to
ReadToEndof the spawned process’s standard output, which will block until that process closes its standard output stream. In this case, this will be when the process terminates.What you need to do instead is redirect the standard output and read chunks out of it as they become available using
BeginOutputReadLine.For example: