开发者

destroy class inside other class

in this code I'm trying to Listening to multi connection at the same time with Asynchronous

Please how to destroy _receivedata class at this end of this code

class x

{

Thread t1; 
        int flag = 0;
        string receivedPath = "yok";
        public delegate void MyDelegate();
        BinaryWriter writer;

 private void sent_file_Load(object sender, EventArgs e)
        {
            t1 = new Thread(new ThreadStart(StartListening));
            t1.Start();

        }


 public class StateObject
        {
            // Client socket.
            public Socket workSocket = null;
            public const int BufferSize = 1024;
            // Receive buffer.
            public byte[] buffer = new byte[BufferSize];
        }


   public void StartListening()
        {
            byte[] bytes = new Byte[1024];
            IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 8221);
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                listener.Bind(ipEnd);
                listener.Listen(100);
                if (listener.Connected)
                {
                    allDone.Close();
                }
                else
                {
                    while (true)
                    {
                        allDone.Reset();
                        listener.BeginAccept(new AsyncCallback(AcceptConn), listener);
                        allDone.WaitOne();
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }

        public void AcceptConn(IAsyncResult ar)
        {
            allDone.Set();
            Socket listener = (Socket)ar.AsyncState;
            Socket handler = listener.EndAccept(ar);
            StateObject state = new StateObject();
            state.workSocket = handler;
            _receivedata rceve = new _receivedata();
            handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(rceve.ReceiveData), state);
            flag = 0;
        }



        public class _receivedata
        {

            public void开发者_JAVA技巧 ReceiveData(IAsyncResult ar)
            {
                some code

                //how to destroy this instance of class at the end of this void

            }
        }


   }

when i use this code it destroy all class ,class _receivedata and class x

 public class _receivedata : IDisposable
        {

            public void ReceiveData(IAsyncResult ar)
            {
                some code

               Dispose() ;  //it destroy all class ,class _receivedata and class x



            }




        ~_receivedata()
            {
                 Dispose(false);
            }
            private bool isDisposed = false;


            protected void Dispose(bool disposing)
            {
                if (disposing)
                {
                    // Code to dispose the managed resources of the class
                }
                // Code to dispose the un-managed resources of the class

                isDisposed = true;
                 this.Dispose(disposing);

            }

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

    }


Not sure what exactly you are looking for, but C# does have destructors. Despite the name, these act more like finalizers than real destructors (which, by the way don't exist in .NET).

Example:

public class _receivedata
{
  public void ReceiveData(IAsyncResult ar)
  {
  }

  public ~ReceiveData()
  {
     // Finalization logic
  }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜