View Full Version : Realtime reading mencoder output in c# withouth eating too much cpu
Lolitka
24th May 2007, 09:39
Hello i have a new question:
I'm trying to read mencoder output in real-time in c# in one my simple application and i have problem that 2 threads with lowest priority eat about 20% of cpu and because of that mplayer run slower (even when it's thread have high priority).
maybe i'm stupid, but i can't figure out how to fix it with sleep.
So how do you handle it? Or where is it exactly in megui source?
Or want someone help me if i post here my ugly code?
Doom9
24th May 2007, 09:50
how do you do it then? There are several approaches that all work
1) blocking reads (sr.ReadLine() blocks until you get something, then you dispatch it asap and you get back to blocking) with a separate thread per reader.
2) You can configure the process to throw evens whenever a line has been read so you have two event handlers, one for a stdout line that the process writes, and one for a stderr line that the process writes
3) the way megui does it (or used to? it's been so long I don't recall if it ever changed again).. similar to 1 but with a little twist in it.
Lolitka
24th May 2007, 12:40
Ok i have 2 threads that cycle in infinite loop reading std output and std err line. And one thread that starts mencoder.
here is code:
public void ReadOutput()
{
string str;
try
{
while (proc != null)
{
try
{
while ((str = proc.StandardOutput.ReadLine()) != null)
{
this.SendTextRe2(str);
}
}
catch (Exception e)
{
Application.Exit();
}
}
}catch (Exception e)
{
}
}
public void ReadErrOutput()
{
string str;
try
{
while (proc != null)
{
try
{
while ((str = proc.StandardError.ReadLine()) != null)
{
this.SendTextRe3(str);
}
}
catch (Exception e)
{
Application.Exit();
}
}
}catch (Exception e)
{
}
}
public void ExecMencoder()
{
proc = System.Diagnostics.Process.Start(procInfo);
t2.Start();
t3.Start();
proc.EnableRaisingEvents = true;
proc.WaitForExit();
t2.Suspend();
t3.Suspend();
this.SetTextLbl4("Done");
System.Windows.Forms.MessageBox.Show("Done");
}
EDIT
now when i look it here i see something that maybe causing it
try
{
while ((str = proc.StandardError.ReadLine()) != null)
{
this.SendTextRe3(str);
}
}
catch (Exception e)
{
Application.Exit();
}
what do you think? :)
EDIT 2: it was stupid idea, because it never catch error - if it does application.exit should happen and it doesn't.
Lolitka
29th May 2007, 13:06
well i found that if i remove this.SendTextRe3(str); and this.SendTextRe2(str); delegates then it use about 0% percent of cpu
private void SendTextRe3(string text)
{
if (this.richTextBox3.InvokeRequired)
{
SendTextCallbackRe3 re3 = new SendTextCallbackRe3(SendTextRe3);
this.Invoke(re3, new object[] { text });
}
else
{
if (j == 500)
{
this.richTextBox2.Text = null;
}
this.richTextBox3.AppendText(text + Environment.NewLine);
j++;
}
}
Lolitka
29th May 2007, 14:30
so i solved it like this
while ((str = proc.StandardOutput.ReadLine()) != null)
{
j++;
if (j == 500)
{
this.SendTextRe2(str);
j = 0;
}
}
not super clean solution, but works ...
ronnylov
21st June 2007, 15:36
This article may help you:
http://www.developerfusion.co.uk/show/4519/
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.