StringReader.ReadToEndAsync 方法

定義

非同步讀取從當前位置到字串末尾的所有字元,並以單一字串回傳。

public:
 override System::Threading::Tasks::Task<System::String ^> ^ ReadToEndAsync();
public override System.Threading.Tasks.Task<string> ReadToEndAsync();
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task<string> ReadToEndAsync();
override this.ReadToEndAsync : unit -> System.Threading.Tasks.Task<string>
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.ReadToEndAsync : unit -> System.Threading.Tasks.Task<string>
Public Overrides Function ReadToEndAsync () As Task(Of String)

傳回

一個代表非同步讀取操作的任務。 TResult參數值包含一個字串,字元從當前位置到字串末尾。

屬性

例外狀況

字串讀取器已被處理。

讀取器目前正被先前的讀取操作使用。

範例

以下範例展示了如何非同步讀取整串字串。

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");

            using (StringReader reader = new StringReader(stringToRead.ToString()))
            {
                string readText = await reader.ReadToEndAsync();
                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.ReadToEndAsync()
            Console.WriteLine(readText)
        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,仍會同步拋出。 關於儲存的例外,請參見由 ReadToEnd()拋出的例外。

適用於