应该关闭它后,我手动配置的插座? [英] Should I manually dispose the socket after closing it?

查看:168
本文介绍了应该关闭它后,我手动配置的插座?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该还是叫的Dispose()我的插座的关闭它?



例如:

  mySocket.Shutdown(SocketShutdown.Both); 
mySocket.Close();
mySocket.Dispose(); //冗余?



我不知道,因为的 MSDN文档说以下内容:



<块引用>

关闭套接字连接并释放所有相关的资源。



解决方案

调用关闭内部调用Dispose,所以你并不需要通话双方。从 .net反射

 公共无效关闭()
{
如果(s_LoggingEnabled)
{
Logging.Enter(Logging.Sockets,对此,关闭(串)空值);
}
((IDisposable接口)本).Dispose();
如果(s_LoggingEnabled)
{
Logging.Exit(Logging.Sockets,对此,关闭(串)NULL);
}
}

如果有可能,你应该使用使用模式,使您随时来电,无论处置可能发生的任何异常。


Should I still call Dispose() on my socket after closing it?

For example:

mySocket.Shutdown(SocketShutdown.Both);
mySocket.Close();
mySocket.Dispose(); // Redundant?

I was wondering because the MSDN documentation says the following:

Closes the Socket connection and releases all associated resources.

解决方案

Calling Close internally calls Dispose so you don't need to call both. From .NET Reflector:

public void Close()
{
    if (s_LoggingEnabled)
    {
        Logging.Enter(Logging.Sockets, this, "Close", (string) null);
    }
    ((IDisposable)this).Dispose();
    if (s_LoggingEnabled)
    {
        Logging.Exit(Logging.Sockets, this, "Close", (string) null);
    }
}

If possible you should use the using pattern so that you always call Dispose regardless of any exceptions that might occur.

这篇关于应该关闭它后,我手动配置的插座?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆