StringReader.ReadAsync Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Overload
| Nome | Descrizione |
|---|---|
| ReadAsync(Memory<Char>, CancellationToken) |
Legge in modo asincrono tutti i caratteri dalla stringa di input, a partire dalla posizione corrente e sposta la posizione corrente alla fine della stringa di input. |
| ReadAsync(Char[], Int32, Int32) |
Legge un numero massimo di caratteri specificato dalla stringa corrente in modo asincrono e scrive i dati in un buffer, a partire dall'indice specificato. |
ReadAsync(Memory<Char>, CancellationToken)
Legge in modo asincrono tutti i caratteri dalla stringa di input, a partire dalla posizione corrente e sposta la posizione corrente alla fine della stringa di input.
public override System.Threading.Tasks.ValueTask<int> ReadAsync(Memory<char> buffer, System.Threading.CancellationToken cancellationToken = default);
override this.ReadAsync : Memory<char> * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<int>
Public Overrides Function ReadAsync (buffer As Memory(Of Char), Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of Integer)
Parametri
- cancellationToken
- CancellationToken
Token da monitorare per le richieste di annullamento. Il valore predefinito è None.
Valori restituiti
Attività che rappresenta l'operazione di lettura asincrona. Il valore del TResult parametro contiene il numero totale di caratteri letti nel buffer.
Eccezioni
Il token di annullamento è stato annullato. Questa eccezione viene archiviata nell'attività restituita.
Si applica a
ReadAsync(Char[], Int32, Int32)
Legge un numero massimo di caratteri specificato dalla stringa corrente in modo asincrono e scrive i dati in un buffer, a partire dall'indice specificato.
public:
override System::Threading::Tasks::Task<int> ^ ReadAsync(cli::array <char> ^ buffer, int index, int count);
public override System.Threading.Tasks.Task<int> ReadAsync(char[] buffer, int index, int count);
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task<int> ReadAsync(char[] buffer, int index, int count);
override this.ReadAsync : char[] * int * int -> System.Threading.Tasks.Task<int>
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.ReadAsync : char[] * int * int -> System.Threading.Tasks.Task<int>
Public Overrides Function ReadAsync (buffer As Char(), index As Integer, count As Integer) As Task(Of Integer)
Parametri
- buffer
- Char[]
Quando termina, questo metodo contiene la matrice di caratteri specificata con i valori compresi tra index e (index + count - 1) sostituiti dai caratteri letti dall'origine corrente.
- index
- Int32
Posizione in in buffer cui iniziare la scrittura.
- count
- Int32
Numero massimo di caratteri da leggere. Se la fine della stringa viene raggiunta prima che il numero specificato di caratteri venga scritto nel buffer, il metodo restituisce .
Valori restituiti
Attività che rappresenta l'operazione di lettura asincrona. Il valore del TResult parametro contiene il numero totale di byte letti nel buffer. Il valore del risultato può essere minore del numero di byte richiesti se il numero di byte attualmente disponibili è minore del numero richiesto oppure può essere 0 (zero) se è stata raggiunta la fine della stringa.
- Attributi
Eccezioni
buffer è null.
index o count è negativo.
La somma di index e count è maggiore della lunghezza del buffer.
Il lettore di stringhe è stato eliminato.
Il lettore è attualmente in uso da un'operazione di lettura precedente.
Esempio
Nell'esempio seguente viene illustrato come leggere i primi 23 caratteri di una stringa in modo asincrono.
using System;
using System.IO;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
ReadCharacters();
}
static async void ReadCharacters()
{
string stringToRead = "Some characters to read but not all";
char[] charsRead = new char[stringToRead.Length];
using (StringReader reader = new StringReader(stringToRead))
{
await reader.ReadAsync(charsRead, 0, 23);
Console.WriteLine(charsRead);
}
}
}
}
// The example displays the following output:
// Some characters to read
//
Imports System.IO
Module Module1
Sub Main()
ReadCharacters()
End Sub
Async Sub ReadCharacters()
Dim stringToRead = "Some characters to read but not all"
Dim charsRead(stringToRead.Length) As Char
Using reader As StringReader = New StringReader(stringToRead)
Await reader.ReadAsync(charsRead, 0, 23)
Console.WriteLine(charsRead)
End Using
End Sub
End Module
' The example displays the following output:
' Some characters to read
'
Commenti
L'attività viene completata dopo che il numero di caratteri specificato dal count parametro viene letto o viene raggiunta la fine della stringa.
Questo metodo archivia nell'attività che restituisce tutte le eccezioni non di utilizzo che la controparte sincrona del metodo può generare. Se un'eccezione viene archiviata nell'attività restituita, tale eccezione verrà generata quando l'attività è attesa. Le eccezioni di utilizzo, ad esempio ArgumentException, vengono comunque generate in modo sincrono. Per le eccezioni archiviate, vedere le eccezioni generate da Read(Char[], Int32, Int32).