StringReader.ReadLineAsync 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
非同步讀取目前字串中的一行字元,並將資料以字串形式回傳。
public:
override System::Threading::Tasks::Task<System::String ^> ^ ReadLineAsync();
public override System.Threading.Tasks.Task<string> ReadLineAsync();
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task<string> ReadLineAsync();
override this.ReadLineAsync : unit -> System.Threading.Tasks.Task<string>
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.ReadLineAsync : unit -> System.Threading.Tasks.Task<string>
Public Overrides Function ReadLineAsync () As Task(Of String)
傳回
一個代表非同步讀取操作的任務。 參數的TResult值包含字串讀取器的下一行,或若所有字元皆已讀取,則為 。null
- 屬性
例外狀況
下一行的字元數比 Int32.MaxValue 還多。
字串讀取器已被處理。
讀取器目前正被先前的讀取操作使用。
範例
以下範例展示了如何非同步地一次讀取一行字串。
using System;
using System.IO;
using System.Text;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
ReadCharacters();
}
static async void ReadCharacters()
{
StringBuilder stringToRead = new StringBuilder();
stringToRead.AppendLine("Characters in 1st line to read");
stringToRead.AppendLine("and 2nd line");
stringToRead.AppendLine("and the end");
string readText;
using (StringReader reader = new StringReader(stringToRead.ToString()))
{
while ((readText = await reader.ReadLineAsync()) != null)
{
Console.WriteLine(readText);
}
}
}
}
}
// The example displays the following output:
//
// Characters in 1st line to read
// and 2nd line
// and the end
//
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
ReadCharacters()
End Sub
Async Sub ReadCharacters()
Dim stringToRead = New StringBuilder()
stringToRead.AppendLine("Characters in 1st line to read")
stringToRead.AppendLine("and 2nd line")
stringToRead.AppendLine("and the end")
Using reader As StringReader = New StringReader(stringToRead.ToString())
Dim readText As String = Await reader.ReadLineAsync()
While Not IsNothing(readText)
Console.WriteLine(readText)
readText = Await reader.ReadLineAsync()
End While
End Using
End Sub
End Module
' The example displays the following output:
'
' Characters in 1st line to read
' and 2nd line
' and the end
'
備註
此方法在任務中儲存所有非使用例外,該方法的同步對應程式可拋出這些例外。 如果回傳的任務中儲存了例外,該例外會在等待任務時拋出。 使用例外錯誤,例如 ArgumentException,仍會同步拋出。 關於儲存的例外,請參見由 ReadLine()拋出的例外。