Reading multiple times from the same stream in C #
I want to read the output of my program several times. Some things like if I pass X I get output and I show it and then again if I pass Y I get output and I show it. without restarting the process. to try i made c program
#include<stdio.h>
int main()
{
int i;
int j;
while(scanf("%d", &i))
{
for(j = 0; j<=i;j++)
printf("%d\n",j);
}
return 0;
}
and now I interact with it from C # where when I enter text into a textbox it is passed through a standard input redirection (streamwriter) to the program and for reading the output I call it standard output (stream reader). readtoend ().
But that doesn't work for me. Since it goes to waitstate until the stream returns, the read indicator will end.
How can I achieve this?
I also tried asynchronous reading when I call the beginoutputread method, but then I won't know when the reading is finished! One way might help me add a marker to my original program to indicate that the output has ended for the current input. Is there any other way for me to achieve this?
a source to share
Quck and Dirty: This one works with some minor glitches. Try to improve it because I am leaving the office :)
ProcessStartInfo psi = new ProcessStartInfo(@"c:\temp\testC.exe");
psi.CreateNoWindow = true;
psi.RedirectStandardError = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
string input = "";
ConsoleColor fc = Console.ForegroundColor;
StreamWriter sw = p.StandardInput;
StreamReader sr = p.StandardOutput;
char[] buffer = new char[1024];
int l = 0;
do
{
Console.Write("Enter input: ");
input = Console.ReadLine();
int i = Convert.ToInt32(input);
sw.Write(i);
sw.Write(sw.NewLine);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write(">> ");
l = sr.Read(buffer, 0, buffer.Length);
for (int n = 0; n < l; n++)
Console.Write(buffer[n] + " ");
Console.WriteLine();
Console.ForegroundColor = fc;
} while (input != "10");
Console.WriteLine("Excution Finished. Press Enter to close.");
Console.ReadLine();
p.Close();
PS: - I created a console exe in vs2008 and copied it to the c: \ temp folder under the name testC.exe.
a source to share