System.IO.StreamWriter.Flush C# (CSharp) Method

Flush() private method

private Flush ( bool flushStream, bool flushEncoder ) : void
flushStream bool
flushEncoder bool
return void
        private void Flush(bool flushStream, bool flushEncoder)
        {
            // flushEncoder should be true at the end of the file and if
            // the user explicitly calls Flush (though not if AutoFlush is true).
            // This is required to flush any dangling characters from our UTF-7 
            // and UTF-8 encoders.  
            if (_stream == null)
            {
                throw new ObjectDisposedException(null, SR.ObjectDisposed_WriterClosed);
            }

            // Perf boost for Flush on non-dirty writers.
            if (_charPos == 0 && !flushStream && !flushEncoder)
            {
                return;
            }

            if (!_haveWrittenPreamble)
            {
                _haveWrittenPreamble = true;
                byte[] preamble = _encoding.GetPreamble();
                if (preamble.Length > 0)
                {
                    _stream.Write(preamble, 0, preamble.Length);
                }
            }

            int count = _encoder.GetBytes(_charBuffer, 0, _charPos, _byteBuffer, 0, flushEncoder);
            _charPos = 0;
            if (count > 0)
            {
                _stream.Write(_byteBuffer, 0, count);
            }
            // By definition, calling Flush should flush the stream, but this is
            // only necessary if we passed in true for flushStream.  The Web
            // Services guys have some perf tests where flushing needlessly hurts.
            if (flushStream)
            {
                _stream.Flush();
            }
        }

Same methods

StreamWriter::Flush ( ) : void

Usage Example

        //OnWriteToStreamAsync(Type type, object value, Stream stream, HttpContentHeaders contentHeaders, FormatterContext formatterContext, TransportContext transportContext)
        public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
        {
            string uri = HttpContext.Current.Request.Url.Query;
            string method = HttpContext.Current.Request.HttpMethod;
            if(HttpContext.Current.Request.HttpMethod =="GET")
            {

                return Task.Factory.StartNew(() =>
                {
                    var writer = new StreamWriter(writeStream);
                    var query = HttpUtility.ParseQueryString(uri);
                    string callback = query[CallbackQueryParameter];
                    writer.Write(callback + "(");
                    writer.Flush();

                    base.WriteToStreamAsync(type, value, writeStream, content, transportContext).Wait();

                    writer.Write(")");
                    writer.Flush();
                });
            }
            else
            {
                return base.WriteToStreamAsync(type, value, writeStream, content, transportContext);
            }
        }
All Usage Examples Of System.IO.StreamWriter::Flush