Debug.Flush 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
排清輸出緩衝區,並導致緩衝的數據寫入至 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 ()
- 屬性
範例
下列範例會建立名為 TextWriterTraceListenermyTextListener。
myTextListener 使用 一個 FileStream 被呼叫 myFileStream 來寫入一個名為 TestFile.txt的檔案。 範例中會建立串流,若有檔案就開啟或建立新檔案,寫一行文字到檔案,然後清除並關閉輸出。
// 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
備註
除非你明確呼叫 Flush 或 Close,否則 flush 串流不會沖走其底層編碼器。 設定 AutoFlush 為 true 表示資料會從緩衝區沖洗到串流,但編碼器狀態不會被沖洗。 這讓編碼器能保留其狀態(部分字元),以便正確編碼下一個字元區塊。 此情境影響 UTF8 與 UTF7,因為某些字元只能在編碼器接收相鄰字元後再編碼。