使用NetworkStream.BeginRead和NetworkStream.EndRead实现超时

Ver*_*rax 4 .net c# asynchronous networkstream

我编写了以下函数来使用NetworkStream异步读取函数(BeginReadEndRead)实现超时功能.它工作正常,直到我注释掉该行Trace.WriteLine("bytesRead: " + bytesRead);.为什么?

private int SynchronousRead(byte[] buffer, int count)
{
    int bytesRead = 0;
    bool success = false;
    IAsyncResult result = null;

    result = _stream.BeginRead(
        buffer, 0, count,
        delegate(IAsyncResult r)
        {
            bytesRead = _stream.EndRead(r);
        },
        null);

    success = result.AsyncWaitHandle.WaitOne(_ioTmeoutInMilliseconds, false);

    if (!success)
    {
        throw new TimeoutException("Could not read in the specfied timeout.");
    }

    //If I remove this line, bytesRead is always 0
    Trace.WriteLine("bytesRead: " + bytesRead);

    return bytesRead;
}
Run Code Online (Sandbox Code Playgroud)

万一你想知道,我必须这样做,因为我最终需要针对.Net Compact Framework 3.5,它不支持NetworkStream.ReadTimeoutNetworkStream.WriteTimeout属性.

Han*_*ant 5

一个有趣的线程错误.在发出等待句柄信号后,将分配bytesRead变量.有两件事可能会出错:方法在分配之前返回.或者线程读取过时的值,因为它们在WaitOne()调用之后没有内存障碍.Trace语句修复了这个问题,因为它将主线程延迟了足够长的时间以允许写入变量.它有一个内部锁,可确保缓存一致.

您需要一个额外的AutoResetEvent来表示写入了bytesRead变量.