2
      Process cmdProcess = new Process
        {
            StartInfo =
            {
                FileName = "cmd.exe",
                WorkingDirectory = "C:/Program Files/R/R-3.0.2/bin",
                Arguments = " /C R --slave --args $@ ",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                RedirectStandardInput = true,
                CreateNoWindow = true,
            }
        };            

        cmdProcess.Start();
        cmdProcess.BeginOutputReadLine();

        foreach (var item in File.ReadAllLines(“Audit.R”))
        {
            cmdProcess.StandardInput.WriteLine(item);
            cmdProcess.OutputDataReceived += cmdProcess_OutputDataReceived;

        }

    }

And I try to get the output using the following event.

    public void cmdProcess_OutputDataReceived(object sender,DataReceivedEventArgs e)
    {
        this.ResultText += e.Data + "\n";
    }

I get the output repeated. This is my input R script,

getwd()
data<-read.csv("Audit.csv")
str(data)

Can anyone help me on this issue? Thanks

Edwin Vivek N
  • 564
  • 8
  • 28

1 Answers1

1

IT looks like you are adding outputdatareceived event handler multiple times (resulting in multiple calls per event). I might rewrite it to something like this, but you could substitute your R file IO below instead of my hard coded string:

    public class TestRProcess
    {
        public StringBuilder output = new StringBuilder();
        public StringBuilder error = new StringBuilder();

        string script =
@"getwd()
a<-1:3
b<-4:6
data<-data.frame(a,b)
str(data)
q()
";

        public void Process()
        {
            ProcessStartInfo ProcessParameters = new ProcessStartInfo(@"C:\Program Files\R\R-3.1.1\bin\R.exe")
            {
                Arguments = "--vanilla --slave",
                CreateNoWindow = true,
                UseShellExecute = false,
                RedirectStandardError = true,
                RedirectStandardInput = true,
                RedirectStandardOutput = true
            };

            int Max_Time = 10000;


            Process p = new Process();
            p.StartInfo = ProcessParameters;

            //Borrowed: http://stackoverflow.com/questions/139593/processstartinfo-hanging-on-waitforexit-why
            output = new StringBuilder();
            error = new StringBuilder();

            using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
            using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
            {
                p.OutputDataReceived += (sender, e) =>
                {
                    if (e.Data == null)
                    {
                        outputWaitHandle.Set();
                    }
                    else
                    {
                        output.AppendLine(e.Data);
                    }
                };
                p.ErrorDataReceived += (sender, e) =>
                {
                    if (e.Data == null)
                    {
                        errorWaitHandle.Set();
                    }
                    else
                    {
                        error.AppendLine(e.Data);
                    }
                };

                p.Start();

                p.StandardInput.WriteLine(script);

                p.BeginOutputReadLine();
                p.BeginErrorReadLine();

                if (p.WaitForExit(Max_Time) &&
                    outputWaitHandle.WaitOne(Max_Time) &&
                    errorWaitHandle.WaitOne(Max_Time))
                {

                }
                else
                {
                    throw new Exception("Timed Out");
                }
            }
        }
Mike Burr
  • 146
  • 1
  • 7