我现在正在尝试将信息从子进程(即管道)传递到其父进程,而该父进程只是一种形式。当它们是独立的而不是子进程时,我已经使命名管道在彼此之间工作,但是当我尝试使用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
}
}
}
}
}
答案 0 :(得分:0)
因此,这可能是我遇到过的最严重的错误,它是如此肮脏,以至于它的金钱使锈迹斑斑。我也不是受过正式训练的程序员,所以方程式就是这样。
此行:Console.SetWindowSize(100, 15);
导致了该错误。一旦将其从子进程中删除,一切都会恢复正常。我不知道为什么。