Reply to thread

Something really strange:


[code=csharp]

        protected void WaitForData()

        {

            if(this.SocketCallback == null)

            {

                this.SocketCallback = new AsyncCallback(OnDataReceive);

            }

           

            ServerSocket state = new ServerSocket();

            state.Socket = this.Socket;

           

            this.Socket.BeginReceive(state.buffer1, 0, state.buffer1.Length, SocketFlags.None, this.SocketCallback, state);

        }

       

        public void OnDataReceive(IAsyncResult result)

        {

            ServerSocket socket = (ServerSocket) result.AsyncState;

            int bufferOffset = this.Socket.EndReceive(result);

           

            if(bufferOffset > 0 && bufferOffset != socket.buffer1.Length)

            {

                byte[] temp = new byte[bufferOffset];

                for(int i = 0; i < temp.Length; i++)

                {

                    temp[i] = socket.buffer1[i];

                }

               

                this.buffer1 = temp;

                temp = null;

            }

        }

[/code]


I get an ObjectDisposedException at this line:

[code=csharp]

            int bufferOffset = this.Socket.EndReceive(result);

[/code]


But:

[code=csharp]

        protected void WaitForData()

        {

            if(this.SocketCallback == null)

            {

                this.SocketCallback = new AsyncCallback(OnDataReceive);

            }

           

            ServerSocket state = new ServerSocket();

            state.Socket = this.Socket;

           

            this.Socket.BeginReceive(state.buffer1, 0, state.buffer1.Length, SocketFlags.None, this.SocketCallback, state);

            MessageBox.Show("Foo");

        }

       

        public void OnDataReceive(IAsyncResult result)

        {

            ServerSocket socket = (ServerSocket) result.AsyncState;

            int bufferOffset = this.Socket.EndReceive(result);

           

            if(bufferOffset > 0 && bufferOffset != socket.buffer1.Length)

            {

                byte[] temp = new byte[bufferOffset];

                for(int i = 0; i < temp.Length; i++)

                {

                    temp[i] = socket.buffer1[i];

                }

               

                this.buffer1 = temp;

                temp = null;

            }

        }

[/code]

(Note de MessageBox in WaitForData())


Works perfect.


Whats wrong?


Back
Top