0

I need to keep PipeStrem open and not to close it, i.e. get rid off using. I tried to keep it open, but get Exception Cannot Access closed strem How can I solve it? This is my code now:

    public static void StartServer()
    {

        //NamedPipeServerStream pipeStream = new NamedPipeServerStream("PipeTo" + Process.GetCurrentProcess().Id.ToString());

        while(true)
        {
            using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("PipeTo" + Process.GetCurrentProcess().Id.ToString()))
            {

                Console.WriteLine("[Server] Pipe Created, the current process ID is {0};\n[Server] The current process name is {1}", Process.GetCurrentProcess().Id.ToString(), Process.GetCurrentProcess().ProcessName);
                try { pipeStream.WaitForConnection(); }
                catch
                {
                    _worker = new BackgroundWorker();
                    _worker.DoWork += _worker_DoWork;
                }
                Console.WriteLine("[Server] Connected Successfully");

                using (StreamReader sr = new StreamReader(pipeStream))
                {                        
                    string pathToExe = string.Empty;

                    while ((pathToExe = sr.ReadLine()) != null)
                    {
                        Process.Start(pathToExe);                            
                    }
                }
            }
        }
    }

1 Answers1

0

using on either the stream itself, or the StreamReader will cause it to close.

For more information: Does disposing streamreader close the stream?

Community
  • 1
  • 1
GeirGrusom
  • 999
  • 5
  • 18