English 中文(简体)
C# Keeping Pipes Open
原标题:
  • 时间:2009-11-11 22:34:24
  •  标签:
  • c#
  • pipe

I ve Got two Programs (Server / Client) I m trying to setup IPC for them (They both run on the same box) Using System.IO.Pipes & Net 3.5

When I call ComOpen, it opens the Pipe correctly, sends the Process ID to the server, but then the Pipe closes and I get an error when it tries to send "Second Write Test"

So Question is. How do I keep the Pipe open for the Life of the Program? (I use the Process ID on the server to close everything down if the Client crashes)

private static StreamWriter MyWriter;
private static StreamReader MyReader;
private static NamedPipeClientStream IPCPipe = new NamedPipeClientStream(".", "MyPipe", PipeDirection.InOut);


    public static bool MyWrite(string DataOut)
    {
        bool ValidPipeOut = false;
        if(ValidComPort)
        try
        {
            // Send Data
            using (QstWriter = new StreamWriter(IPCPipe))
            {
                QstWriter.AutoFlush = true;
                QstWriter.WriteLine(QstDataOut);
                QstWriter.Close();
                QstWriter.Dispose();
            }
            ValidPipeOut = true;
        }
        catch
        {
            ValidPipeOut = false;
        }
        return ValidPipeOut;
    }


    public static bool ComOpen()
    {
        ValidComPort = true;

        try { IPCPipe.Connect(1000); }
        catch (Exception ex)
        {
            string Erroris;
            Erroris = ex.Message;
            if (Erroris == "Already in a connected state.")
            {
                // We re Already Connected, Ignore this error.
                ValidComPort = true;
            }
            else
            {
                ValidComPort = false;
                MessageBox.Show(Erroris);
            }
        }
        if (ValidComPort)
        {
            string ClientProcessID = System.Diagnostics.Process.GetCurrentProcess().Id.ToString();
            MyReader = new StreamReader(IPCPipe);
            ValidComPort = MyWrite(ClientProcessID);
            ValidComPort = MyWrite("Second Write Test");
        }
        return ValidComPort;
    }
最佳回答

The problem is the following line:

using (QstWriter = new StreamWriter(IPCPipe))

At the end of the using statement, the StreamWriter will be disposed and that will in turn dispose the IPCPipe. You are also explicitly calling Dispose and Close on QstWriter, which will close the pipe too.

To fix this, remove the using statement and the calls to Dispose and Close on QstWriter. And assign+initialize QstWriter only once.

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签