StringWriter.WriteAsync Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Écrit des données dans la chaîne de manière asynchrone.
Surcharges
| Nom | Description |
|---|---|
| WriteAsync(Char) |
Écrit un caractère dans la chaîne de façon asynchrone. |
| WriteAsync(String) |
Écrit une chaîne dans la chaîne actuelle de façon asynchrone. |
| WriteAsync(ReadOnlyMemory<Char>, CancellationToken) |
Écrit de façon asynchrone une région de mémoire de caractères dans la chaîne. |
| WriteAsync(Char[], Int32, Int32) |
Écrit un sous-tableau de caractères dans la chaîne de manière asynchrone. |
WriteAsync(Char)
Écrit un caractère dans la chaîne de façon asynchrone.
public:
override System::Threading::Tasks::Task ^ WriteAsync(char value);
public override System.Threading.Tasks.Task WriteAsync(char value);
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task WriteAsync(char value);
override this.WriteAsync : char -> System.Threading.Tasks.Task
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.WriteAsync : char -> System.Threading.Tasks.Task
Public Overrides Function WriteAsync (value As Char) As Task
Paramètres
- value
- Char
Caractère à écrire dans la chaîne.
Retours
Tâche qui représente l’opération d’écriture asynchrone.
- Attributs
Exceptions
L’enregistreur de chaînes est supprimé.
L’enregistreur de chaînes est actuellement utilisé par une opération d’écriture précédente.
Exemples
L’exemple suivant montre comment écrire des caractères à l’aide de la WriteAsync(Char) méthode.
using System;
using System.Text;
using System.IO;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
WriteCharacters();
}
static async void WriteCharacters()
{
StringBuilder stringToWrite = new StringBuilder("Characters in StringBuilder");
stringToWrite.AppendLine();
using (StringWriter writer = new StringWriter(stringToWrite))
{
UnicodeEncoding ue = new UnicodeEncoding();
char[] charsToAdd = ue.GetChars(ue.GetBytes("and chars to add"));
foreach (char c in charsToAdd)
{
await writer.WriteAsync(c);
}
Console.WriteLine(stringToWrite.ToString());
}
}
}
}
// The example displays the following output:
//
// Characters in StringBuilder
// and chars to add
//
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
WriteCharacters()
End Sub
Async Sub WriteCharacters()
Dim stringToWrite As StringBuilder = New StringBuilder("Characters in StringBuilder")
stringToWrite.AppendLine()
Using writer As StringWriter = New StringWriter(stringToWrite)
Dim ue As UnicodeEncoding = New UnicodeEncoding()
Dim charsToAdd() = ue.GetChars(ue.GetBytes("and chars to add"))
For Each c As Char In charsToAdd
Await writer.WriteAsync(c)
Next
Console.WriteLine(stringToWrite.ToString())
End Using
End Sub
End Module
' The example displays the following output:
'
' Characters in StringBuilder
' and chars to add
'
Remarques
Cette méthode stocke dans la tâche toutes les exceptions non-utilisation que l’équivalent synchrone de la méthode peut lever. Si une exception est stockée dans la tâche retournée, cette exception est levée lorsque la tâche est attendue. Les exceptions d’utilisation, telles que ArgumentException, sont toujours levées de façon synchrone. Pour les exceptions stockées, consultez les exceptions levées par Write(Char).
S’applique à
WriteAsync(String)
Écrit une chaîne dans la chaîne actuelle de façon asynchrone.
public:
override System::Threading::Tasks::Task ^ WriteAsync(System::String ^ value);
public override System.Threading.Tasks.Task WriteAsync(string value);
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task WriteAsync(string value);
override this.WriteAsync : string -> System.Threading.Tasks.Task
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.WriteAsync : string -> System.Threading.Tasks.Task
Public Overrides Function WriteAsync (value As String) As Task
Paramètres
- value
- String
Chaîne à écrire. Si value c’est nullle cas, rien n’est écrit dans le flux de texte.
Retours
Tâche qui représente l’opération d’écriture asynchrone.
- Attributs
Exceptions
L’enregistreur de chaînes est supprimé.
L’enregistreur de chaînes est actuellement utilisé par une opération d’écriture précédente.
Exemples
L’exemple suivant montre comment écrire une chaîne à l’aide de la WriteAsync(String) méthode.
using System;
using System.Text;
using System.IO;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
WriteCharacters();
}
static async void WriteCharacters()
{
StringBuilder stringToWrite = new StringBuilder("Characters in StringBuilder");
stringToWrite.AppendLine();
using (StringWriter writer = new StringWriter(stringToWrite))
{
await writer.WriteAsync("and add characters through StringWriter");
Console.WriteLine(stringToWrite.ToString());
}
}
}
}
// The example displays the following output:
//
// Characters in StringBuilder
// and add characters through StringWriter
//
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
WriteCharacters()
End Sub
Async Sub WriteCharacters()
Dim stringToWrite As StringBuilder = New StringBuilder("Characters in StringBuilder")
stringToWrite.AppendLine()
Using writer As StringWriter = New StringWriter(stringToWrite)
Await writer.WriteAsync("and add characters through StringWriter")
Console.WriteLine(stringToWrite.ToString())
End Using
End Sub
End Module
' The example displays the following output:
'
' Characters in StringBuilder
' and add characters through StringWriter
'
Remarques
Cette méthode stocke dans la tâche toutes les exceptions non-utilisation que l’équivalent synchrone de la méthode peut lever. Si une exception est stockée dans la tâche retournée, cette exception est levée lorsque la tâche est attendue. Les exceptions d’utilisation, telles que ArgumentException, sont toujours levées de façon synchrone. Pour les exceptions stockées, consultez les exceptions levées par Write(String).
S’applique à
WriteAsync(ReadOnlyMemory<Char>, CancellationToken)
Écrit de façon asynchrone une région de mémoire de caractères dans la chaîne.
public override System.Threading.Tasks.Task WriteAsync(ReadOnlyMemory<char> buffer, System.Threading.CancellationToken cancellationToken = default);
override this.WriteAsync : ReadOnlyMemory<char> * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Overrides Function WriteAsync (buffer As ReadOnlyMemory(Of Char), Optional cancellationToken As CancellationToken = Nothing) As Task
Paramètres
- buffer
- ReadOnlyMemory<Char>
Région de mémoire de caractères à écrire dans la chaîne.
- cancellationToken
- CancellationToken
Jeton à surveiller pour les demandes d’annulation. La valeur par défaut est None.
Retours
Tâche qui représente l’opération d’écriture asynchrone.
Exceptions
Le jeton d’annulation a été annulé. Cette exception est stockée dans la tâche retournée.
S’applique à
WriteAsync(Char[], Int32, Int32)
Écrit un sous-tableau de caractères dans la chaîne de manière asynchrone.
public:
override System::Threading::Tasks::Task ^ WriteAsync(cli::array <char> ^ buffer, int index, int count);
public override System.Threading.Tasks.Task WriteAsync(char[] buffer, int index, int count);
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task WriteAsync(char[] buffer, int index, int count);
override this.WriteAsync : char[] * int * int -> System.Threading.Tasks.Task
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.WriteAsync : char[] * int * int -> System.Threading.Tasks.Task
Public Overrides Function WriteAsync (buffer As Char(), index As Integer, count As Integer) As Task
Paramètres
- buffer
- Char[]
Tableau de caractères à partir duquel écrire des données.
- index
- Int32
Position dans la mémoire tampon à laquelle commencer la lecture des données.
- count
- Int32
Nombre maximal de caractères à écrire.
Retours
Tâche qui représente l’opération d’écriture asynchrone.
- Attributs
Exceptions
buffer a la valeur null.
Le index plus count est supérieur à la longueur de la mémoire tampon.
index ou count est négatif.
L’enregistreur de chaînes est supprimé.
L’enregistreur de chaînes est actuellement utilisé par une opération d’écriture précédente.
Exemples
L’exemple suivant montre comment écrire des caractères à l’aide de la WriteAsync(Char[], Int32, Int32) méthode.
using System;
using System.Text;
using System.IO;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
WriteCharacters();
}
static async void WriteCharacters()
{
StringBuilder stringToWrite = new StringBuilder("Characters in StringBuilder");
stringToWrite.AppendLine();
using (StringWriter writer = new StringWriter(stringToWrite))
{
UnicodeEncoding ue = new UnicodeEncoding();
char[] charsToAdd = ue.GetChars(ue.GetBytes("and chars to add"));
await writer.WriteAsync(charsToAdd, 0, charsToAdd.Length);
Console.WriteLine(stringToWrite.ToString());
}
}
}
}
// The example displays the following output:
//
// Characters in StringBuilder
// and chars to add
//
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
WriteCharacters()
End Sub
Async Sub WriteCharacters()
Dim stringToWrite As StringBuilder = New StringBuilder("Characters in StringBuilder")
stringToWrite.AppendLine()
Using writer As StringWriter = New StringWriter(stringToWrite)
Dim ue As UnicodeEncoding = New UnicodeEncoding()
Dim charsToAdd() = ue.GetChars(ue.GetBytes("and chars to add"))
Await writer.WriteAsync(charsToAdd, 0, charsToAdd.Length)
Console.WriteLine(stringToWrite.ToString())
End Using
End Sub
End Module
' The example displays the following output:
'
' Characters in StringBuilder
' and chars to add
'
Remarques
Cette méthode stocke dans la tâche toutes les exceptions non-utilisation que l’équivalent synchrone de la méthode peut lever. Si une exception est stockée dans la tâche retournée, cette exception est levée lorsque la tâche est attendue. Les exceptions d’utilisation, telles que ArgumentException, sont toujours levées de façon synchrone. Pour les exceptions stockées, consultez les exceptions levées par Write(Char[], Int32, Int32).