C# .Net BinaryWriter在检查流位置时自动刷新流

C# .Net BinaryWriter在检查流位置时自动刷新流,c#,.net,stream,.net-4.5,flush,C#,.net,Stream,.net 4.5,Flush,有没有其他人看到这个或者知道如何关闭它 我有一段代码,定期检查BinaryWriter中流的位置。每次调用BinaryWriter.BaseStream.Position方法都会导致调用该流的Flush方法 我尝试使用BinaryWriter和StreamWriter,但只有BinaryWriter演示了这种行为 下面是一些示例代码: namespace FlushaholicStreams { class Program { static void Main(s

有没有其他人看到这个或者知道如何关闭它

我有一段代码,定期检查BinaryWriter中流的位置。每次调用BinaryWriter.BaseStream.Position方法都会导致调用该流的Flush方法

我尝试使用BinaryWriter和StreamWriter,但只有BinaryWriter演示了这种行为

下面是一些示例代码:

namespace FlushaholicStreams
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var stream = new PrivateStream())
            using (var writer = new BinaryWriter(stream))
            {
                var data = "hi there, this is a really long string. Very very very long";

                for (int i = 0; i < 19; i++)
                {
                    data += data;
                }

                for (int j = 0; j < 8; j++)
                {
                    var bytes = Encoding.ASCII.GetBytes(data);
                    writer.Write(bytes);
                    var position = writer.BaseStream.Position;
                    Console.WriteLine("The position was {0}", position);
                }
            }

            Console.WriteLine("All done");
        }
    }

    class PrivateStream : MemoryStream
    {
        public int FlushCount = 0;
        public int CloseCount = 0;

        public override void Close()
        {
            this.CloseCount++;
            Console.WriteLine("Closing the stream");
            base.Close();
        }

        public override void Flush()
        {
            this.FlushCount++;
            Console.WriteLine("Flushing the stream");
            base.Flush();
        }
    }
}

我使用的是.NET4.5,这个框架很容易刷新。这很糟糕,因为它强制磁盘访问。(主观说明:这是一个设计缺陷。)


为自己编写一个包含另一个流的
。在包装器类中,您重写必要的方法,以在实例字段中维护自己的
位置。这样就根本不需要访问包装流的
位置
成员。

看起来BinaryWriter类会强制过度刷新,并且无法覆盖它。我将保留对原始流的引用,并直接检查其位置

我在这里找到了(据称)源代码:


我以前没注意到。。。但这有点道理。如果您还有数据要刷新到流中,您如何确定流的实际位置?您是否严格地只从流中读取数据?您的问题没有意义。我正在查看的源代码中没有任何内容会导致调用
Flush()
方法。请提供准确说明您要求的内容。@LeandroTaset不,我正在向流中写信。流正在通过网络写入数据,过度刷新导致性能问题您只需将writer.BaseStream属性getter移到循环之外,就可以感觉更好。但是,除非您对基本流类型有特定的了解,否则这不是正确的做法。如果这是一个记忆流也没关系,但是担心刷新是完全没有意义的。
Flushing the stream
The position was 30932992
Flushing the stream
The position was 61865984
Flushing the stream
The position was 92798976
Flushing the stream
The position was 123731968
Flushing the stream
The position was 154664960
Flushing the stream
The position was 185597952
Flushing the stream
The position was 216530944
Flushing the stream
The position was 247463936
Flushed the stream 8 times
Closing the stream
Closing the stream
All done
/* 
* Returns the stream associate with the writer. It flushes all pending
* writes before returning. All subclasses should override Flush to 
* ensure that all buffered data is sent to the stream.
*/
public virtual Stream BaseStream {
  get { 
    Flush();
    return OutStream; 
  } 
}

// Clears all buffers for this writer and causes any buffered data to be
// written to the underlying device.
public virtual void Flush()
{ 
  OutStream.Flush();
}