I'm trying to pass information from a child process, that is a pipe, to its parent process that is just a form at this point. I have gotten my named pipes to work between each other when they are independent and not child processes however when I try to use the pipeServer as a child process, it(the child pipeserver process ) starts and then is closed and I can't figure out why. It is also not producing any output even though I have it redirected to my debug.
My pipe server should close after receiving 3 messages from the pipeClient. Currently, it seems to start as it is called and then closes a couple of seconds later automatically when it should be waiting to receive a message from the pipe client.
Any and all help/direction is appreciated.
here is my code:
From the form calling the process
private void pipeB_Button_Click(object sender, EventArgs e)
{
using (Process process = new Process())
{
process.StartInfo.FileName = @"\\Mac\Home\Desktop\myPathName\TestNamedPipeB\TestNamedPipeB\bin\Debug\TestNamedPipeB.exe";
process.StartInfo.UseShellExecute = false; // needed somehow
//process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start(); // this is where it is starting and closing.
process.WaitForExit();
StreamReader reader = process.StandardOutput;
string s = process.StandardOutput.ReadToEnd();
Debug.WriteLine("s: " + s);
string output = reader.ReadToEnd(); // used
pipeConversation.Append(output);
Debug.WriteLine("pipeConversation: " + pipeConversation.ToString());
}
}
my server pipe code
namespace TestNamedPipeB
{
class Program
{
static void Main(string[] args)
{
Console.SetWindowSize(100, 15);
StartServer();
}
static void StartServer()
{
var server = new NamedPipeServerStream("test-pipe");
if (server.IsConnected == false)
{
Console.WriteLine("Currently waiting for a client to connect...");
}
server.WaitForConnection();
StreamReader reader = new StreamReader(server);
StreamWriter writer = new StreamWriter(server);
Console.Write("A client has connected, awaiting greeting from client... \n");
string emptyArgsString = "PipeA sent an empty message. PipeB (this program) is assuming pipeA has no arugments and is closing";
int readerCounter = 0;
int readerMaxInt = 3; // edit me to an int greather than 1 if you want to test
while (readerCounter < readerMaxInt)
{
var line = reader.ReadLine();
if (line == null)
{
Console.WriteLine(emptyArgsString);
Debug.WriteLine(emptyArgsString);
MessageBox.Show(emptyArgsString, "M3dida", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
}
Console.WriteLine("PipeA said: " + line);
Debug.WriteLine("PipeA said: " + line);
readerCounter++;
if (readerMaxInt > 1)
{
writer.WriteLine(Console.ReadLine()); //<-- needed to send a response back if sending multiple messages
writer.Flush(); // onus changes to other pipe
}
}
}
}
}