我正在編寫的程序是在Linux中使用FIFO管道進行進程間通信。充其量只是黑客,但不管我有問題。單聲道在嘗試打開StreamWriter到命名管道時掛起
if (!File.Exists(Path.GetTempPath() + "gminput.pipe"))
{
ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = "/usr/bin/mkfifo", Arguments = Path.GetTempPath() + "gminput.pipe", };
Process proc = new Process() { StartInfo = startInfo, };
proc.Start();
proc.WaitForExit();
}
if (!File.Exists(Path.GetTempPath() + "gmoutput.pipe"))
{
ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = "/usr/bin/mkfifo", Arguments = Path.GetTempPath() + "gmoutput.pipe", };
Process proc = new Process() { StartInfo = startInfo, };
proc.Start();
proc.WaitForExit();
}
using (StreamWriter outputPipe = new StreamWriter(Path.GetTempPath() + "gmoutput.pipe"))
using (StreamReader inputPipe = new StreamReader(Path.GetTempPath() + "gminput.pipe"))
{
Console.WriteLine("This code is never reached!");
}
我所做的只是檢查管道是否已經存在,如果沒有,請致電mkfifo創建它。這部分似乎工作正常,命名管道正確創建。每當我嘗試打開它們(無論是StreamWriter,StreamReader,還是兩者),該程序只是掛起。沒有錯誤或任何東西。它也掛在調試器中。
最好的部分是......它曾經工作。我有進程間通信工作,然後它只是莫名其妙地停止。除了你在這裏看到的東西,我重新評論了一切,重新啓動了我的系統,重新創建了管道等,但都無濟於事。是什麼賦予了?我的代碼有什麼問題,或者系統上的其他內容有干擾?
我不知道它是否會對您有所幫助,但我在過去曾將問題連接在一起的問題。有一天它工作,下一個不行!對我來說,解決方案是在路徑上使用Path.Combine(...),這可以阻止不一致的行爲。 –