我正在尝试将信息从子进程(即管道)传递到其父进程,此时它只是一个表单。当我的命名管道是独立的而不是子进程时,我已经让它们在彼此之间工作,但是当我尝试将 pipeServer 用作子进程时,它(子 pipeserver 进程)启动然后关闭,我不知道出为什么。即使我将它重定向到我的调试,它也不会产生任何输出。
在收到来自 pipeClient 的 3 条消息后,我的管道服务器应该关闭。目前,它似乎在被调用时开始,然后在它应该等待接收来自管道客户端的消息时自动关闭几秒钟。
任何和所有的帮助/方向表示赞赏。
这是我的代码:
从表单调用流程
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());
}
}
我的服务器管道代码
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
}
}
}
}
}