释放一个插座/事件/ ummaged代码与处置正确的技术/模式敲定 [英] the correct technique for releasing a socket/event/ummaged code with the dispose/finalize pattern

查看:143
本文介绍了释放一个插座/事件/ ummaged代码与处置正确的技术/模式敲定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何实现时,我的类包含一个插座及放大器的Dispose模式;事件?

How to implement the Dispose pattern when my class contains a socket & event?

它应该是这样的?

class MyClass
{
   Socket m_ListenerSocket = new Socket();
   book m_Disposed=false;

   public void Dispose()
   {
      Dispose(true);
      GC.SuppressFinalize(this);
   }

   private void Dispose(bool isDisposing)
   {
      if (!m_Disposed)
      {
          if (isDisposing)
          {
              if (m_ListenerSocket != null)
              {
                   m_ListenerSocket.Dispose();
                   innerClass.Notify -= Notify;
              }
          }
        //finalized unmanged code here
        m_Disposed = true;
      }
  }

  ~MyClass()
  {
       Dispose(false);
  }
}



我糊涂了......是套接字类Winsock的托管代码C#版本?
,所以应该在用户调用处置的情况下,被释放(isDisposing是真的)
什么有关事件处理程序?

I'm confused... is socket class is "managed code c# version of winSock"? so it should be released in case of user called dispose ("isDisposing IS true") what about event handlers?

因此在敲定评论部分应释放仅Inptr对象?
感谢。

so at finalized comment section should free only Inptr objects? Thanks.

推荐答案

我觉得有很多办法对付一次性的对象,无论它们有事件或不

I think there are many ways to deal with disposable objects, regardless if they have events or not.

只是胡乱猜测,但如果你从.NET框架了一个类,这个类有一个Dispose()方法,你几乎可以说,这被托管的代码,所以你是安全的,只为了呼唤,而不是自己创造的析构函数Dispose方法。这是相当obiquitous因为当你在我下面的例子中看到的,你也可以实现IDisposable接口在自己的类,然后以适当的方式处理您的内部对象。

Is only a wild guess, but if you took a class from the .net framework and this class has a Dispose() method, you can pretty much say that it is managed code, so you are safe just to call the Dispose method instead of creating the destructor by yourself. This is rather obiquitous because as you see in my example below, you can also implement IDisposable interface in your own classes and then dispose your internal objects in a proper way.

莫非?这对您有所帮助。

Could this be helpful to you?

public class MyClassWithSocket :IDisposable 
{

    Socket myInternalSocket = null;

    public void methodThatUsesSocket()
    {
        using (var mySocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream , ProtocolType.Tcp))
        {
        //do something with socket 
        //this will be disposed automatically

        }
    }

    public void methodThatUsesInternalSocket() 
    {
        myInternalSocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
        //do other things
    }

    public static Socket SomethingThatReturnsSocket()
    {

        Socket tempSocket =  new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);

        return tempSocket;
    }


    public void Dispose()
    {
        myInternalSocket.Dispose();
    }
}

这篇关于释放一个插座/事件/ ummaged代码与处置正确的技术/模式敲定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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