线程上的NetworkStream AccessViolationException

时间:2014-04-17 10:01:19

标签: c#

我有一个从TCPclient获取流的小程序,但是我收到一条错误消息:""。

代码如下:

  AutoResetEvent _waitEvent = new AutoResetEvent(false);
    bool _isConnected = false;
    private TcpClient tcpClient;
    public void Connect() 
    {
        Thread t = new Thread(new ThreadStart(() =>
        {

            try
            {
                while (true)
                {

                     tcpClient = new TcpClient();
                    tcpClient.Client.NoDelay = true;
                    tcpClient.Client.Blocking = true;
                    tcpClient.Client.DontFragment = false;
                    tcpClient.Client.LingerState.Enabled = false;
                    tcpClient.Client.LingerState.LingerTime = 0;
                    tcpClient.Client.UseOnlyOverlappedIO = true;
                    try
                    {
                        tcpClient.Connect("myhost", 8001);
                        NetworkStream networkStream = tcpClient.GetStream();
                        byte[] buffer = new byte[tcpClient.ReceiveBufferSize];
                        networkStream.BeginRead(buffer, 0, buffer.Length, ReadCallback, buffer);
                        var wasSet = _waitEvent.WaitOne(TimeSpan.FromSeconds(30));
                        if (wasSet)
                        {
                            if (_isConnected)
                            {
                                return;
                            }
                            else
                            {
                                Thread.Sleep(TimeSpan.FromSeconds(10));
                                continue;
                            }
                        }
                        else
                        {
                            Thread.Sleep(TimeSpan.FromSeconds(10));
                            continue;
                        }
                    }
                    catch (Exception ex)
                    {
                        Thread.Sleep(TimeSpan.FromSeconds(10));
                        continue;
                    }
                }
            }
            catch (ThreadAbortException)
            {
                return;
            }
        }));
        t.Start();
    }

    private void ReadCallback(IAsyncResult result)
    {
        try
        {
            int read;
            NetworkStream networkStream;
            try
            {
                networkStream = tcpClient.GetStream();
                read = networkStream.EndRead(result);
            }
            catch (Exception ex)
            {
                Close();
                return;
            }

            if (read == 0)
            {
                Close();
                return;
            }
            if (!_isConnected)
            {
                _isConnected = true;
                _waitEvent.Set();
            }
            byte[] buffer = result.AsyncState as byte[];
            networkStream.BeginRead(buffer, 0, buffer.Length, ReadCallback, buffer);
        }
        catch { };
    }

    public void Close()
    {
        try
        {
            _isConnected = false;
            if (tcpClient != null)
                tcpClient.Close();
        }
        catch
        {

        }
    }

在行中:

networkStream.BeginRead(buffer, 0, buffer.Length, ReadCallback, buffer);

我收到错误消息:" AccessViolationException未处理 - 尝试读取或写入受保护的内存。这通常表明其他内存已损坏。"

如何解决?谢谢!

1 个答案:

答案 0 :(得分:0)

for user2191454,试试这个:

    private void ReadCallback(IAsyncResult result)
{
    try
    {
        int read;
        NetworkStream networkStream;
        try
        {
            networkStream = tcpClient.GetStream();
            read = networkStream.EndRead(result);
        }
        catch (Exception ex)
        {
            Close();
            return;
        }

        if (read == 0)
        {
            Close();
            return;
        }
        if (!_isConnected)
        {
            _isConnected = true;
            _waitEvent.Set();
        }
        try
        {
            byte[] buffer = result.AsyncState as byte[];
            try
            {
                ParseData(buffer, read);
            }
            catch (Exception e1)
            {
            }
            //Then start reading from the network again.
            networkStream.BeginRead(buffer, 0, buffer.Length, ReadCallback, buffer);
        }
        catch (Exception ex)
        {
            Close();
            return;
        }
    }
    catch (Exception ex)
    {
    }
}