4
  public void ExecuteProcessChain(string[] asProcesses, string sInRedirect, string sOutRedirect)
    {
            Process p1 = new Process();
            p1.StartInfo.UseShellExecute = false;
            p1.StartInfo.RedirectStandardOutput = true;
            p1.StartInfo.FileName = asProcesses[0];
            p1.Start();
            StreamReader sr = p1.StandardOutput;
            string s, xxx = "";
            while ((s = sr.ReadLine()) != null)
                Console.WriteLine("sdfdsfs");
                //xxx += s+"\n";
            p1.StartInfo.RedirectStandardInput = true;
            p1.StartInfo.RedirectStandardOutput = false;
            p1.StartInfo.FileName = asProcesses[1];
            p1.Start();
            StreamWriter sw = p1.StandardInput;
            sw.Write(xxx);
            sw.Close();
            sr.Close();

    }

I am trying to execute "calc|calc" but when I do so , it gets stuck at the line while ((s = sr.ReadLine()) != null) and only after I close the calculator the code continues. I need both calculators to work together. do you have idea how to do that?

Jacob
  • 77,566
  • 24
  • 149
  • 228
Shahar Shmaram
  • 233
  • 1
  • 4
  • 10
  • Why are you using the statement `while ((s = sr.ReadLine()) != null)` ? – Searock Mar 02 '11 at 17:39
  • because i need to read the data, for example : ls|sort , i need to read the ls lines and sort them. – Shahar Shmaram Mar 02 '11 at 19:21
  • Is this, by chance, the same homework that Roy Gavrielov has?: [how can I check if the process got output?](http://stackoverflow.com/questions/5171945/how-can-i-check-if-the-process-got-output/5172500) As even the variable names are the same, the answer I posted to his question should work for you as well. – Tamschi Mar 03 '11 at 01:14
  • By the way, you don't have to close the reader and writer, as they don't bind any unmanaged resources. As a rule of thumb, you should only close a stream, reader or writer if you created it. If it's IDisposable (applies to all Streams and most Readers and Writers), you should instead use a [using statement](http://msdn.microsoft.com/en-us/library/yh598w02.aspx) whenever possible. This has the same effect as closing a Stream but is easier to read and prevents a few common mistakes. – Tamschi Mar 03 '11 at 01:22
  • A few other small things I noticed: `string s, xxx = "";` only sets `xxx` to `""`, s is uninitialized. The parameters of the function are named according to [Systems Hungarian](http://en.wikipedia.org/wiki/Hungarian_notation#Systems_vs._Apps_Hungarian). This may be part of your assignment but is [generally seen as bad practice, especially in C#](http://en.wikipedia.org/wiki/Hungarian_notation#Notable_opinions). Here's an interesting article on the subject: [Making Wrong Code Look Wrong](http://www.joelonsoftware.com/articles/Wrong.html) – Tamschi Mar 03 '11 at 01:40

2 Answers2

1

ReadLine is reading from the output of the first calc. Calc doesn't send any output. So, ReadLine will never return and thus the next calc will not start. When the first calc terminates, ReadLine can no longer read from the first calc, so returns null. After it has returned, the code can start the second calc.

You can either not read from the first calc or read asynchronously. You might want to refer to Async ReadLine on how to read asynchronously.

You could alternatively start the second calc with p2 before you start calling ReadLine.

Community
  • 1
  • 1
Fun Mun Pieng
  • 6,751
  • 3
  • 28
  • 30
0

Why not use threading?

Consider this: Put each calc into a thread and then start them both. After that make the program wait for them. Only after both threads are done with their jobs (read data) then you can continue.

Remember that threads cannot directly alter data from another thread so I might suggest the use of Invoke or static variables, depending on what you might need.

If possible, you can make use of Task/Parallel library which already have some useful methods to help you on this.

Background Worker is too a way to go.

Anderson Matos
  • 3,132
  • 1
  • 23
  • 33