线程上出现NetworkStream AccessViolationException

本文关键字:NetworkStream AccessViolationException 线程 | 更新日期: 2023-09-27 17:58:48

我有一个小程序,它从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未处理-试图读取或写入受保护的内存。这通常表明其他内存已损坏。"

我该怎么修?谢谢

线程上出现NetworkStream AccessViolationException

对于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)
    {
    }
}