[英]c# - Why is my child process pipe closing automatically?
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.我正在尝试将来自子进程的信息(即 pipe)传递给它的父进程,此时它只是一个表单。 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.
当我的命名管道是独立的而不是子进程时,我已经让它们在彼此之间工作,但是当我尝试将 pipeServer 用作子进程时,它(子 pipeserver 进程)启动然后关闭,我想不通出为什么。 It is also not producing any output even though I have it redirected to my debug.
即使我将它重定向到我的调试,它也不会产生任何 output。
My pipe server should close after receiving 3 messages from the pipeClient.我的 pipe 服务器在收到来自 pipeClient 的 3 条消息后应该关闭。 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.
目前,它似乎在被调用时启动,然后在几秒钟后自动关闭,此时它应该等待接收来自 pipe 客户端的消息。
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我的服务器 pipe 代码
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
}
}
}
}
}
So this is probably the worst bug I've ever come across and is so incredibly dirty that it gives a rusty nail a run for its money.所以这可能是我遇到过的最糟糕的虫子,而且脏得令人难以置信,以至于生锈的钉子都可以赚到钱。 I'm also not a formally trained programmer so there's that to take into the equation.
我也不是受过正式培训的程序员,所以要考虑到这一点。
this line: Console.SetWindowSize(100, 15);
这一行:
Console.SetWindowSize(100, 15);
was causing the bug.导致了这个错误。 Once I removed it from the child process, everything worked again.
一旦我从子进程中删除它,一切都会再次运行。 I have no idea why.
我不知道为什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.