【问题标题】:can i check a tcpclient is disposed or not in tcp ip in c#我可以在 c# 中检查 tcpclient 是否已在 tcp ip 中处理吗
【发布时间】:2022-01-26 18:03:26
【问题描述】:

我想为 tcp/ip 创建一个服务器

我有这个代码:

if (myTcpClient.Available == 0)
{
}

当客户处理后我收到此错误:

System.Object Disposed Exception: 'Cannot access a disposed object.

我可以检查客户是否已处置吗?谢谢 我可以检查客户是否已处置?谢谢 我可以检查客户是否已处置?谢谢 我可以检查客户是否已处置?谢谢 我可以检查客户是否已处置?谢谢 我可以检查客户是否已处置?谢谢

【问题讨论】:

    标签: c# tcp tcpclient tcplistener


    【解决方案1】:

    首先——没有理由多次发送垃圾邮件。

    但答案就是对 ObjectDisposedException 使用 try/catch,这几乎是最简单的方法。

    try 
    {
        /* access myTcpClient */
    }
    catch (ObjectDisposedException)
    {
        /* do some behaviour based on exception */
    }
    

    仅此而已:)

    【讨论】:

      猜你喜欢
      • 2013-01-10
      • 1970-01-01
      • 2014-11-14
      • 1970-01-01
      • 2015-11-19
      • 1970-01-01
      • 2020-07-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多