유용한 구현이 될 것이므로 이상하게 생각됩니다. 그러나 간단하게 작성해야합니다. 이런 식으로 뭔가 작업을해야합니다 :
public class BlockingStream: Stream
{
private readonly Stream _stream;
public BlockingStream(Stream stream)
{
if(!stream.CanSeek)
throw new ArgumentException("Stream must support seek", "stream");
_stream = stream;
}
public override void Flush()
{
lock (_stream)
{
_stream.Flush();
Monitor.Pulse(_stream);
}
}
public override long Seek(long offset, SeekOrigin origin)
{
lock (_stream)
{
long res = _stream.Seek(offset, origin);
Monitor.Pulse(_stream);
return res;
}
}
public override void SetLength(long value)
{
lock (_stream)
{
_stream.SetLength(value);
Monitor.Pulse(_stream);
}
}
public override int Read(byte[] buffer, int offset, int count)
{
lock (_stream)
{
do
{
int read = _stream.Read(buffer, offset, count);
if (read > 0)
return read;
Monitor.Wait(_stream);
} while (true);
}
}
public override void Write(byte[] buffer, int offset, int count)
{
lock (_stream)
{
long currentPosition = _stream.Position;
_stream.Position = _stream.Length;
_stream.Write(buffer, offset, count);
_stream.Position = currentPosition;
Monitor.Pulse(_stream);
}
}
public override bool CanRead
{
get
{
lock (_stream)
{
return _stream.CanRead;
}
}
}
public override bool CanSeek
{
get
{
lock (_stream)
{
return _stream.CanSeek;
}
}
}
public override bool CanWrite
{
get
{
lock (_stream)
{
return _stream.CanWrite;
}
}
}
public override long Length
{
get
{
lock (_stream)
{
return _stream.Length;
}
}
}
public override long Position
{
get
{
lock (_stream)
{
return _stream.Position;
}
}
set
{
lock (_stream)
{
_stream.Position = value;
Monitor.Pulse(_stream);
}
}
}
}
고마워, 나는 이것과 비슷한 것을했지만 리더 레벨에서했다. 이것은보다 유연하게 보입니다. –
이것을 구현하여 잘 작동합니다. 하나의 문제 : StreamReader와 StreamWriter를 BlockingStream에 연결하는 경우 작성기에서 AutoFlush를 설정합니다. –