【问题标题】:How to abort an async wait for a pipe connection? [duplicate]如何中止异步等待管道连接? [复制]
【发布时间】:2017-07-25 16:04:58
【问题描述】:

在下面的代码中

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,但在 Connected 中调用的 EndWaitForConnection 处出现此异常。

System.Core.dll 中出现“System.ObjectDisposedException”类型的未处理异常 附加信息:无法访问封闭的管道。

有什么想法吗?

我改成这样了:

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();
        }
    }
}

【问题讨论】:

  • 据我所知,关闭等待管道是中止等待的常用方法。没有其他 API 可以做到这一点,这与使用 Socket 类和“接受”操作需要做的事情是一致的。您在使用该方法时实际上有什么具体问题吗?
  • 我描述了。我得到了例外。
  • 不要将其标记为重复或给我链接。
  • "我描述。我得到异常" -- 正常异常的描述是not a 问题描述.
  • 问题是我得到了我完全给出了它的描述的异常。这就是问题。我不知道如何避免它,为什么会这样。然后,再一次,你没有告诉我重复的帖子是什么。

标签: c# pipe


【解决方案1】:

切换到异步版本:BeginWaitForConnection。

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

检查一下

【讨论】:

  • 他们已经打电话给BeginWaitForConnection
猜你喜欢
  • 2013-02-26
  • 1970-01-01
  • 2021-05-25
  • 2018-08-13
  • 1970-01-01
  • 2021-07-08
  • 1970-01-01
  • 2023-01-30
  • 1970-01-01
相关资源
最近更新 更多