Export (0) Print
Expand All

Debug.Flush Method

Flushes the output buffer and causes buffered data to write to the Listeners collection.

[Visual Basic]
<Conditional("DEBUG")>
Public Shared Sub Flush()
[C#]
[Conditional("DEBUG")]
public static void Flush();
[C++]
[Conditional("DEBUG")]
public: static void Flush();
[JScript]
public
   Conditional("DEBUG")
static function Flush();

Remarks

Flushing the stream will not flush its underlying encoder unless you explicitly call Flush or Close. Setting AutoFlush to true means that data will be flushed from the buffer to the stream, but the encoder state will not be flushed. This allows the encoder to keep its state (partial characters) so that it can encode the next block of characters correctly. This scenario affects UTF8 and UTF7 where certain characters can only be encoded after the encoder receives the adjacent character or characters.

Example

The following example creates a TextWriterTraceListener named myTextListener. myTextListener uses a StreamWriter called myOutputWriter to write to a file named TestFile.txt. The example creates the file, stream, and text writer, writes one line of text to the file, and then flushes and closes the output.

[Visual Basic] 
Overloads Public Shared Sub Main(args() As String)
    ' Create a file for output named TestFile.txt.
    Dim myFileName As String = "TestFile.txt"
    If Not File.Exists(myFileName) Then
        File.Create(myFileName)
    End If 
    
    ' Assign output file to output stream.
    Dim myOutputWriter As StreamWriter
    myOutputWriter = File.AppendText(myFileName)
    
    ' Create a new text writer using the output stream, and
    ' add it to the trace listeners. 
    Dim myTextListener As New TextWriterTraceListener(myOutputWriter)
    Debug.Listeners.Add(myTextListener)
    
    ' Write output to the file.
    Debug.WriteLine("Test output")
    
    ' Flush and close the output stream.
    Debug.Flush()
    Debug.Close()
End Sub 'Main

[C#] 
public static void Main(string[] args) {
    // Create a file for output named TestFile.txt.
    String myFileName = "TestFile.txt";
    if(!File.Exists(myFileName)) 
    File.Create(myFileName);

 
    // Assign output file to output stream.
    StreamWriter myOutputWriter;
    myOutputWriter = File.AppendText(myFileName);
 
    /* Create a new text writer using the output stream, and 
     * add it to the trace listeners. */
    TextWriterTraceListener myTextListener = new 
       TextWriterTraceListener(myOutputWriter);
    Debug.Listeners.Add(myTextListener);
  
    // Write output to the file.
    Debug.WriteLine("Test output");
 
    // Flush and close the output stream.
    Debug.Flush();
    Debug.Close();
 }


[C++] 
#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Diagnostics;

int main(void)
{
    // Create a file for output named TestFile.txt.
    String * myFileName = S"TestFile.txt";
    if ( !File::Exists(myFileName) ) 
        File::Create(myFileName);

    // Assign output file to output stream.
    StreamWriter * myOutputWriter;
    myOutputWriter = File::AppendText(myFileName);
 
    /* Create a new text writer using the output stream, and 
     * add it to the trace listeners. */
    TextWriterTraceListener * myTextListener = new 
       TextWriterTraceListener(myOutputWriter);
    Debug::Listeners->Add(myTextListener);
  
    // Write output to the file.
    Debug::WriteLine(S"Test output");
 
    // Flush and close the output stream.
    Debug::Flush();
    Debug::Close();
}

[JScript] 
public static function Main() {
    // Create a file for output named TestFile.txt.
    var myFileName : String = "TestFile.txt";
    if(!File.Exists(myFileName)) 
    File.Create(myFileName);

 
    // Assign output file to output stream.
    var myOutputWriter : StreamWriter;
    myOutputWriter = File.AppendText(myFileName);
 
    /* Create a new text writer import the output stream and 
     * add it to the trace listeners. */
    var myTextListener : TextWriterTraceListener = new 
       TextWriterTraceListener(myOutputWriter);
    Debug.Listeners.Add(myTextListener);
  
    // Write output to the file.
    Debug.WriteLine("Test output");
 
    // Flush and close the output stream.
    Debug.Flush();
    Debug.Close();
 }

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Debug Class | Debug Members | System.Diagnostics Namespace | Debug | Trace | BooleanSwitch | TraceSwitch | TraceListener | DefaultTraceListener | ConditionalAttribute

Show:
© 2014 Microsoft