如何中止异步等待管道连接?

时间:2017-07-25 16:04:59

标签: c# pipe

在以下代码中

namespace ns
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string ProcessCommand(string cmd)
        {
            return "i got " + cmd;
        }

        StreamReader d_sr;
        StreamWriter d_sw;
        NamedPipeServerStream d_pipe;
        IAsyncResult d_ar;

        void Connected(IAsyncResult ar)
        {
            d_pipe.EndWaitForConnection(ar);
            d_sw.AutoFlush = true;
            while (true)
            {
                var cmd = d_sr.ReadLine();
                if (cmd == null)
                    break;
                var answer = ProcessCommand(cmd);
                d_sw.WriteLine(answer);
            }
            d_pipe.Disconnect();
            d_ar = d_pipe.BeginWaitForConnection(Connected, null);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            d_pipe = new NamedPipeServerStream("vatm", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous);
            d_sr = new StreamReader(d_pipe);
            d_sw = new StreamWriter(d_pipe);
            d_ar = d_pipe.BeginWaitForConnection(Connected, null);
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            d_pipe.EndWaitForConnection(d_ar);
            d_pipe.Dispose();
        }
    }
}

当我关闭表单时,它会在EndWaitForConnection中等待。我要告诉管道不要再等待客户端并中止。我尝试了d_pipe.Close()而不是调用EndWaitForConnection,但我在Endcted中调用了EndWaitForConnection这个异常。

  

未处理的类型' System.ObjectDisposedException'发生在System.Core.dll中   附加信息:无法访问已关闭的管道。

有什么想法吗?

我把它更改为:

namespace ns
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string ProcessCommand(string cmd)
        {
            return "i got " + cmd;
        }

        StreamReader d_sr;
        StreamWriter d_sw;
        NamedPipeServerStream d_pipe;
        IAsyncResult d_ar;

        void Connected(IAsyncResult ar)
        {
            try
            {
                d_pipe.EndWaitForConnection(ar);
                d_sw.AutoFlush = true;
                while (true)
                {
                    var cmd = d_sr.ReadLine();
                    if (cmd == null)
                        break;
                    var answer = ProcessCommand(cmd);
                    d_sw.WriteLine(answer);
                }
                d_pipe.Disconnect();
                d_ar = d_pipe.BeginWaitForConnection(Connected, null);
            }
            catch (System.ObjectDisposedException)
            {
                // do nothing
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            d_pipe = new NamedPipeServerStream("vatm", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous);
            d_sr = new StreamReader(d_pipe);
            d_sw = new StreamWriter(d_pipe);
            d_ar = d_pipe.BeginWaitForConnection(Connected, null);
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            d_pipe.Close();
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

切换到异步版本:BeginWaitForConnection。

如果它完成了,你需要一个标志,这样完成处理程序可以调用EndWaitForConnection吸收任何异常并退出(调用End ...以确保能够清除任何资源)。 enter link description here

检查这个