Stream.WriteAsync Stream.WriteAsync Stream.WriteAsync Stream.WriteAsync Method

定义

将字节序列异步写入当前流,并将流的当前位置提升写入的字节数。Asynchronously writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.

重载

WriteAsync(Byte[], Int32, Int32, CancellationToken) WriteAsync(Byte[], Int32, Int32, CancellationToken) WriteAsync(Byte[], Int32, Int32, CancellationToken)

将字节的序列异步写入当前流,将该流中的当前位置向前移动写入的字节数,并监视取消请求。Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.

WriteAsync(ReadOnlyMemory<Byte>, CancellationToken) WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)

将字节的序列异步写入当前流,将该流中的当前位置向前移动写入的字节数,并监视取消请求。Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.

WriteAsync(Byte[], Int32, Int32) WriteAsync(Byte[], Int32, Int32) WriteAsync(Byte[], Int32, Int32) WriteAsync(Byte[], Int32, Int32)

将字节序列异步写入当前流,并将流的当前位置提升写入的字节数。Asynchronously writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.

WriteAsync(Byte[], Int32, Int32, CancellationToken) WriteAsync(Byte[], Int32, Int32, CancellationToken) WriteAsync(Byte[], Int32, Int32, CancellationToken)

将字节的序列异步写入当前流,将该流中的当前位置向前移动写入的字节数,并监视取消请求。Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.

public:
 virtual System::Threading::Tasks::Task ^ WriteAsync(cli::array <System::Byte> ^ buffer, int offset, int count, System::Threading::CancellationToken cancellationToken);
[System.Runtime.InteropServices.ComVisible(false)]
public virtual System.Threading.Tasks.Task WriteAsync (byte[] buffer, int offset, int count, System.Threading.CancellationToken cancellationToken);
abstract member WriteAsync : byte[] * int * int * System.Threading.CancellationToken -> System.Threading.Tasks.Task
override this.WriteAsync : byte[] * int * int * System.Threading.CancellationToken -> System.Threading.Tasks.Task

参数

buffer
Byte[]

从中写入数据的缓冲区。The buffer to write data from.

offset
Int32 Int32 Int32 Int32

buffer 中的从零开始的字节偏移量,从此处开始将字节复制到该流。The zero-based byte offset in buffer from which to begin copying bytes to the stream.

count
Int32 Int32 Int32 Int32

最多写入的字节数。The maximum number of bytes to write.

cancellationToken
CancellationToken CancellationToken CancellationToken CancellationToken

要监视取消请求的标记。The token to monitor for cancellation requests. 默认值为 NoneThe default value is None.

返回

表示异步写入操作的任务。A task that represents the asynchronous write operation.

异常

offsetcount 的总和大于缓冲区长度。The sum of offset and count is larger than the buffer length.

流不支持写入。The stream does not support writing.

流正在由前一次写操作使用。The stream is currently in use by a previous write operation.

注解

WriteAsync方法使你可以在不阻塞主线程的情况下执行占用大量资源的 i/o 操作。The WriteAsync method enables you to perform resource-intensive I/O operations without blocking the main thread. Windows 8.x 应用商店Windows 8.x Store 应用或 桌面desktop 应用中一个耗时的流操作可能阻塞 UI 线程并让您的应用看起来好像不工作时,这种性能的考虑就显得尤为重要了。This performance consideration is particularly important in a Windows 8.x 应用商店Windows 8.x Store app or 桌面desktop app where a time-consuming stream operation can block the UI thread and make your app appear as if it is not working. 在 Visual Basic 和async C#中, async 方法与和await关键字一起使用。The async methods are used in conjunction with the async and await keywords in Visual Basic and C#.

CanWrite使用属性可确定当前实例是否支持写入。Use the CanWrite property to determine whether the current instance supports writing.

如果在操作完成前取消该操作, 则返回的任务包含Canceled Status属性的值。If the operation is canceled before it completes, the returned task contains the Canceled value for the Status property.

有关示例,请参见WriteAsync(Byte[], Int32, Int32)重载。For an example, see the WriteAsync(Byte[], Int32, Int32) overload.

另请参阅

WriteAsync(ReadOnlyMemory<Byte>, CancellationToken) WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)

将字节的序列异步写入当前流,将该流中的当前位置向前移动写入的字节数,并监视取消请求。Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.

public virtual System.Threading.Tasks.ValueTask WriteAsync (ReadOnlyMemory<byte> buffer, System.Threading.CancellationToken cancellationToken = null);
abstract member WriteAsync : ReadOnlyMemory<byte> * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask
override this.WriteAsync : ReadOnlyMemory<byte> * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask

参数

buffer
ReadOnlyMemory<Byte>

从中写入数据的内存区域。The region of memory to write data from.

cancellationToken
CancellationToken CancellationToken CancellationToken CancellationToken

要监视取消请求的标记。The token to monitor for cancellation requests. 默认值为 NoneThe default value is None.

返回

表示异步写入操作的任务。A task that represents the asynchronous write operation.

注解

WriteAsync方法使你可以在不阻塞主线程的情况下执行占用大量资源的 i/o 操作。The WriteAsync method enables you to perform resource-intensive I/O operations without blocking the main thread. Windows 8.x 应用商店Windows 8.x Store 应用或 桌面desktop 应用中一个耗时的流操作可能阻塞 UI 线程并让您的应用看起来好像不工作时,这种性能的考虑就显得尤为重要了。This performance consideration is particularly important in a Windows 8.x 应用商店Windows 8.x Store app or 桌面desktop app where a time-consuming stream operation can block the UI thread and make your app appear as if it is not working. 在 Visual Basic 和async C#中, async 方法与和await关键字一起使用。The async methods are used in conjunction with the async and await keywords in Visual Basic and C#.

CanWrite使用属性可确定当前实例是否支持写入。Use the CanWrite property to determine whether the current instance supports writing.

如果在操作完成前取消该操作, 则返回的任务包含Canceled Status属性的值。If the operation is canceled before it completes, the returned task contains the Canceled value for the Status property.

有关示例,请参见WriteAsync(Byte[], Int32, Int32)重载。For an example, see the WriteAsync(Byte[], Int32, Int32) overload.

WriteAsync(Byte[], Int32, Int32) WriteAsync(Byte[], Int32, Int32) WriteAsync(Byte[], Int32, Int32) WriteAsync(Byte[], Int32, Int32)

将字节序列异步写入当前流,并将流的当前位置提升写入的字节数。Asynchronously writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.

public:
 System::Threading::Tasks::Task ^ WriteAsync(cli::array <System::Byte> ^ buffer, int offset, int count);
[System.Runtime.InteropServices.ComVisible(false)]
public System.Threading.Tasks.Task WriteAsync (byte[] buffer, int offset, int count);
member this.WriteAsync : byte[] * int * int -> System.Threading.Tasks.Task
Public Function WriteAsync (buffer As Byte(), offset As Integer, count As Integer) As Task

参数

buffer
Byte[]

从中写入数据的缓冲区。The buffer to write data from.

offset
Int32 Int32 Int32 Int32

buffer 中的从零开始的字节偏移量,从此处开始将字节复制到该流。The zero-based byte offset in buffer from which to begin copying bytes to the stream.

count
Int32 Int32 Int32 Int32

最多写入的字节数。The maximum number of bytes to write.

返回

表示异步写入操作的任务。A task that represents the asynchronous write operation.

异常

offsetcount 的总和大于缓冲区长度。The sum of offset and count is larger than the buffer length.

流不支持写入。The stream does not support writing.

流正在由前一次写操作使用。The stream is currently in use by a previous write operation.

示例

下面的示例演示如何以异步方式写入文件。The following example shows how to write asynchronously to a file. 该示例使用FileStream类,该类派生Stream自类。The example uses the FileStream class, which derives from the Stream class.

using System;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.IO;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            UnicodeEncoding uniencoding = new UnicodeEncoding();
            string filename = @"c:\Users\exampleuser\Documents\userinputlog.txt";
           
            byte[] result = uniencoding.GetBytes(UserInput.Text);
            
            using (FileStream SourceStream = File.Open(filename, FileMode.OpenOrCreate))
            {
                SourceStream.Seek(0, SeekOrigin.End);
                await SourceStream.WriteAsync(result, 0, result.Length);
            }
        }
    }
}
Imports System.IO
Imports System.Text

Class MainWindow
    Private Async Sub Button_Click(sender As Object, e As RoutedEventArgs)
        Dim uniencoding As UnicodeEncoding = New UnicodeEncoding()
        Dim filename As String = "c:\Users\exampleuser\Documents\userinputlog.txt"

        Dim result As Byte() = uniencoding.GetBytes(UserInput.Text)

        Using SourceStream As FileStream = File.Open(filename, FileMode.OpenOrCreate)
            SourceStream.Seek(0, SeekOrigin.End)
            Await SourceStream.WriteAsync(result, 0, result.Length)
        End Using
    End Sub
End Class

注解

WriteAsync方法使你可以在不阻塞主线程的情况下执行占用大量资源的 i/o 操作。The WriteAsync method enables you to perform resource-intensive I/O operations without blocking the main thread. Windows 8.x 应用商店Windows 8.x Store 应用或 桌面desktop 应用中一个耗时的流操作可能阻塞 UI 线程并让您的应用看起来好像不工作时,这种性能的考虑就显得尤为重要了。This performance consideration is particularly important in a Windows 8.x 应用商店Windows 8.x Store app or 桌面desktop app where a time-consuming stream operation can block the UI thread and make your app appear as if it is not working. 在 Visual Basic 和async C#中, async 方法与和await关键字一起使用。The async methods are used in conjunction with the async and await keywords in Visual Basic and C#.

CanWrite使用属性可确定当前实例是否支持写入。Use the CanWrite property to determine whether the current instance supports writing.

适用于