Udostępnij przez


Debug.Flush Metoda

Definicja

Opróżnia bufor wyjściowy i powoduje buforowane dane do zapisu w kolekcji Listeners .

public:
 static void Flush();
[System.Diagnostics.Conditional("DEBUG")]
public static void Flush ();
[<System.Diagnostics.Conditional("DEBUG")>]
static member Flush : unit -> unit
Public Shared Sub Flush ()
Atrybuty

Przykłady

W poniższym przykładzie zostanie utworzona nazwana TextWriterTraceListenermyTextListener. myTextListener używa wywołania FileStreammyFileStream do zapisu w pliku o nazwie TestFile.txt. Przykład tworzy strumień, otwiera plik, jeśli istnieje lub tworzy nowy, zapisuje jeden wiersz tekstu w pliku, a następnie opróżnia i zamyka dane wyjściowe.

// Specify /DDEBUG when compiling.

#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Diagnostics;

void main()
{
     #if defined(DEBUG)
    // Create a new stream object for an output file named TestFile.txt.
    FileStream^ myFileStream = 
        gcnew FileStream( "TestFile.txt", FileMode::Append );
   
    // Add the stream object to the trace listeners.
    TextWriterTraceListener^ myTextListener = 
        gcnew TextWriterTraceListener( myFileStream );
    Debug::Listeners->Add( myTextListener );
   
    // Write output to the file.
    Debug::WriteLine( "Test output" );
   
    // Flush and close the output stream.
    Debug::Flush();
    Debug::Close();
    #endif
}
// Specify /d:DEBUG when compiling.

using System;
using System.IO;
using System.Diagnostics;

class Test
{
    static void Main()
    {
        // Create a new stream object for an output file named TestFile.txt.
        using (FileStream myFileStream =
            new FileStream("TestFile.txt", FileMode.Append))
        {
            // Add the stream object to the trace listeners.
            TextWriterTraceListener myTextListener =
                new TextWriterTraceListener(myFileStream);
            Debug.Listeners.Add(myTextListener);

            // Write output to the file.
            Debug.WriteLine("Test output");

            // Flush and close the output stream.
            Debug.Flush();
            Debug.Close();
        }
    }
}
' Specify /d:DEBUG=True when compiling.

Imports System.IO
Imports System.Diagnostics

Class Test
    
    Shared Sub Main()
    
        ' Create a new stream object for an output file named TestFile.txt.
        Using myFileStream As New FileStream("TestFile.txt", FileMode.Append)
        
            ' Add the stream object to the trace listeners. 
            Dim myTextListener As New TextWriterTraceListener(myFileStream)
            Debug.Listeners.Add(myTextListener)
            
            ' Write output to the file.
            Debug.WriteLine("Test output")
            
            ' Flush and close the output stream.
            Debug.Flush()
            Debug.Close()
        
        End Using
        
    End Sub

End Class

Uwagi

Opróżnienie strumienia nie spowoduje opróżnienia podstawowego kodera, chyba że jawnie wywołasz Flush metodę lub Close. Ustawienie AutoFlush oznacza true , że dane zostaną opróżnione z buforu do strumienia, ale stan kodera nie zostanie opróżniony. Dzięki temu koder może zachować stan (częściowe znaki), dzięki czemu będzie mógł poprawnie zakodować następny blok znaków. Ten scenariusz ma wpływ na UTF8 i UTF7, gdzie niektóre znaki mogą być kodowane tylko po otrzymaniu sąsiadującego znaku lub znaków przez koder.

Dotyczy

Zobacz też