D
Damien White
I am hoping someone can help me with a threading issue... I have created a
program which runs a console program and displays the output in a
RichTextBox on a Windows form. Here is the code I'm using for the process
to redirect the console output
m_Process = new Process();
m_Process.StartInfo.FileName = Path;
m_Process.StartInfo.Arguments = "\"" + ScriptPath.Text + "\"";
m_Process.StartInfo.UseShellExecute = false;
m_Process.StartInfo.CreateNoWindow = true;
m_Process.StartInfo.RedirectStandardOutput = true;
m_Process.StartInfo.RedirectStandardError = true;
m_Process.Start();
I'm using Everything with it works great, but I'm having a thread syncing
problem. The console program I am running is a script interpreter. The
program runs through the script and lists each line out to the console. My
problem occurs when an error happens. I have 2 threads running to capture
each input stream from the console (Output and Error). My issue is the
program runs listing out the Output from the console (using my output
thread). When an error occurs in the script, the error thread fires. The
error thread prints out the error message, but it's not in-sync with the
output buffer. Basically, it looks something like this....
LINE 1: 1> Good Code (from Output buffer)
LINE 2: 2> Good Code (from Output buffer)
LINE 3: Error Message (from Error buffer)
LINE 4: 3> Good Code (from Output buffer)
LINE 5: 4> Good Code (from Output buffer)
LINE 6: 5> Good Code (from Output buffer)
LINE 7: 6> Bad Code (from Output buffer)
LINE 8: 7> Good Code (from Output buffer)
The error message is printed out before the error "occurs". This obviously
has to do with timing between the two threads. The desired result is to
have the error message (LINE 3 in the example) be printed after the
offending code (LINE 7 in the example). I can't seem to figure out the best
way to do this.
Just for completeness, here are my 2 thread loops
private void StreamError()
{
string strLine = m_Process.StandardError.ReadLine();
try
{
do
{
if (strLine.Length != 0)
{
AddText(strLine);
}
strLine = m_Process.StandardError.ReadLine();
}while(strLine != null);
}
catch (Exception ex)
{
AddErrorText("ERROR STREAM");
AddErrorText(ex.Message);
AddErrorText(ex.StackTrace);
}
}
private void StreamOutput()
{
string strLine = m_Process.StandardOutput.ReadLine();
try
{
do
{
if (strLine.Length != 0)
{
AddText(strLine);
}
strLine = m_Process.StandardOutput.ReadLine();
}while(strLine != null);
}
catch (Exception ex)
{
AddErrorText("OUTPUT STREAM");
AddErrorText(ex.Message);
AddErrorText(ex.StackTrace);
}
}
Thanks in advance for the help!
-Damien
program which runs a console program and displays the output in a
RichTextBox on a Windows form. Here is the code I'm using for the process
to redirect the console output
m_Process = new Process();
m_Process.StartInfo.FileName = Path;
m_Process.StartInfo.Arguments = "\"" + ScriptPath.Text + "\"";
m_Process.StartInfo.UseShellExecute = false;
m_Process.StartInfo.CreateNoWindow = true;
m_Process.StartInfo.RedirectStandardOutput = true;
m_Process.StartInfo.RedirectStandardError = true;
m_Process.Start();
I'm using Everything with it works great, but I'm having a thread syncing
problem. The console program I am running is a script interpreter. The
program runs through the script and lists each line out to the console. My
problem occurs when an error happens. I have 2 threads running to capture
each input stream from the console (Output and Error). My issue is the
program runs listing out the Output from the console (using my output
thread). When an error occurs in the script, the error thread fires. The
error thread prints out the error message, but it's not in-sync with the
output buffer. Basically, it looks something like this....
LINE 1: 1> Good Code (from Output buffer)
LINE 2: 2> Good Code (from Output buffer)
LINE 3: Error Message (from Error buffer)
LINE 4: 3> Good Code (from Output buffer)
LINE 5: 4> Good Code (from Output buffer)
LINE 6: 5> Good Code (from Output buffer)
LINE 7: 6> Bad Code (from Output buffer)
LINE 8: 7> Good Code (from Output buffer)
The error message is printed out before the error "occurs". This obviously
has to do with timing between the two threads. The desired result is to
have the error message (LINE 3 in the example) be printed after the
offending code (LINE 7 in the example). I can't seem to figure out the best
way to do this.
Just for completeness, here are my 2 thread loops
private void StreamError()
{
string strLine = m_Process.StandardError.ReadLine();
try
{
do
{
if (strLine.Length != 0)
{
AddText(strLine);
}
strLine = m_Process.StandardError.ReadLine();
}while(strLine != null);
}
catch (Exception ex)
{
AddErrorText("ERROR STREAM");
AddErrorText(ex.Message);
AddErrorText(ex.StackTrace);
}
}
private void StreamOutput()
{
string strLine = m_Process.StandardOutput.ReadLine();
try
{
do
{
if (strLine.Length != 0)
{
AddText(strLine);
}
strLine = m_Process.StandardOutput.ReadLine();
}while(strLine != null);
}
catch (Exception ex)
{
AddErrorText("OUTPUT STREAM");
AddErrorText(ex.Message);
AddErrorText(ex.StackTrace);
}
}
Thanks in advance for the help!
-Damien