Click here to Skip to main content
15,885,760 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have scenario where in i Need to capture the output from console emitted by perl scripts.
I have used the below code block to accomplish this

C#
myProcessStartInfo = new ProcessStartInfo("C:\\Win16App\\Perl\\bin\\perl.exe");
                myProcessStartInfo.Arguments = scriptPath;
                myProcessStartInfo.UseShellExecute = false;
                // myProcessStartInfo.RedirectStandardOutput = true;
                myProcessStartInfo.RedirectStandardOutput = true;
                myProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                myProcessStartInfo.CreateNoWindow = true;
                myProcess.StartInfo = myProcessStartInfo;
                myProcess.OutputDataReceived += new DataReceivedEventHandler(myProcess_OutputDataReceived);
                                               
 myProcess.Start();
                //  m_progBar.Value = 100;
                // myProcess.StandardOutput.ReadLine();
                myProcess.BeginOutputReadLine();
Posted
Updated 30-Aug-11 4:09am
v2

I see a couple of problems here. First, you should probably redirect not only stdout but also stderr using System.Diagnostics.Process.StandardError. Those multiplatform tool always make it difficult to see it if you redirect the output from console using pipe, but Process makes it right.

Also, I don't see any code where you actually capture StandardInput or StandardError.

Look at this code sample: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx[^].

As you're using BeginOutputReadLine, use this code sample: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline.aspx[^].

I don't think you really need asynchronous read operations though. It's much more straightforward to use a separate thread for creation process and waiting for its end. I think asynchronous operations were only good before threads were introduced. If you use my advice and use thread (which I highly recommend), you can use the pattern shown in a first code sample. In this thread, you can safely use the blocking call to System.Diagnostics.Process.WaitForExit().

See also http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^].

—SA
 
Share this answer
 
Comments
Espen Harlinn 30-Aug-11 19:04pm    
I really liked the BeginOutputReadLine example :)
Sergey Alexandrovich Kryukov 30-Aug-11 22:21pm    
Thank you, Espen. Well, not my example, easy to give out. :-)
--SA
Espen Harlinn 31-Aug-11 10:12am    
Knowing where to find these little gems is still a good thing :)
Try:
using System;
using System.Diagnostics;
 
namespace RedirectExample
{
    class Program
    {
        static void Main()
        {
            Process p = new Process();
            p.StartInfo = new ProcessStartInfo( "C:\\Win16App\\Perl\\bin\\perl.exe", scriptPath )
                {
                    RedirectStandardOutput = true,
                    UseShellExecute = false,
                    CreateNoWindow = true
                };
            p.Start();
            string output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            Console.WriteLine(output);
            Console.Read();
        }
    }
}


Also remember that 64 bit Windows does not support 16 bit applications[^].

Best regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 30-Aug-11 22:22pm    
Exactly. My 5.
--SA
Espen Harlinn 31-Aug-11 10:12am    
Thank you, Sergey!
Try adding this:

C#
myProcessStartInfo.EnableRaisingEvents = true;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900