共用方式為


Stream.WriteAsync 方法

定義

非同步地將一串位元組寫入目前串流,並依照寫入的位元組數將該串流中的當前位置往前推進。

多載

名稱 Description
WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)

非同步地將一串位元組寫入目前串流,依照寫入位元組數將該串流中的當前位置前進,並監控取消請求。

WriteAsync(Byte[], Int32, Int32)

非同步地將一串位元組寫入目前串流,並依照寫入的位元組數將該串流中的當前位置往前推進。

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

非同步地將一串位元組寫入目前串流,依照寫入位元組數將該串流中的當前位置前進,並監控取消請求。

WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)

來源:
Stream.cs
來源:
Stream.cs
來源:
Stream.cs
來源:
Stream.cs
來源:
Stream.cs

非同步地將一串位元組寫入目前串流,依照寫入位元組數將該串流中的當前位置前進,並監控取消請求。

public virtual System.Threading.Tasks.ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, System.Threading.CancellationToken cancellationToken = default);
abstract member WriteAsync : ReadOnlyMemory<byte> * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask
override this.WriteAsync : ReadOnlyMemory<byte> * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask
Public Overridable Function WriteAsync (buffer As ReadOnlyMemory(Of Byte), Optional cancellationToken As CancellationToken = Nothing) As ValueTask

參數

buffer
ReadOnlyMemory<Byte>

是從記憶體中寫入資料的區域。

cancellationToken
CancellationToken

用來監控取消申請的代幣。 預設值為 None

傳回

一個代表非同步寫入操作的任務。

例外狀況

取消令牌也被取消了。 此例外會儲存在回傳的任務中。

備註

WriteAsync 方法能讓你執行資源密集型的 I/O 操作,而不阻塞主執行緒。 在 Windows 8.x 市集應用程式或傳統型應用程式中,此效能考慮特別重要,其中耗時的串流作業可能會封鎖 UI 線程,並讓應用程式看起來好像無法運作。 非同步方法與 asyncawait 關鍵字搭配使用 Visual Basic 和 C# 中。

利用該 CanWrite 屬性判斷目前實例是否支援寫入。

若操作在完成前被取消,回傳的任務會包含 CanceledStatus 屬性的值。

舉例來說,請參考過載。WriteAsync(Byte[], Int32, Int32)

適用於

WriteAsync(Byte[], Int32, Int32)

來源:
Stream.cs
來源:
Stream.cs
來源:
Stream.cs
來源:
Stream.cs
來源:
Stream.cs

非同步地將一串位元組寫入目前串流,並依照寫入的位元組數將該串流中的當前位置往前推進。

public:
 System::Threading::Tasks::Task ^ WriteAsync(cli::array <System::Byte> ^ buffer, int offset, int count);
public System.Threading.Tasks.Task WriteAsync(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
[<System.Runtime.InteropServices.ComVisible(false)>]
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[]

用來寫入資料的緩衝區。

offset
Int32

以零為基礎的位元組偏移量 buffer ,從中開始將位元組複製到串流。

count
Int32

最大可寫入位元組數。

傳回

一個代表非同步寫入操作的任務。

屬性

例外狀況

buffernull

offsetcount 是陰性。

count的和offset大於緩衝區長度。

該課程不支援寫作。

溪流已被處理。

該串流目前正被先前的寫入操作使用。

範例

以下範例展示了如何非同步寫入檔案。 範例中使用了 FileStream 類別,該類別由 Stream 類別衍生而來。

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 操作,而不阻塞主執行緒。 在 Windows 8.x 市集應用程式或傳統型應用程式中,此效能考慮特別重要,其中耗時的串流作業可能會封鎖 UI 線程,並讓應用程式看起來好像無法運作。 非同步方法與 asyncawait 關鍵字搭配使用 Visual Basic 和 C# 中。

利用該 CanWrite 屬性判斷目前實例是否支援寫入。

此方法在任務中儲存所有非使用例外,該方法的同步對應程式可拋出這些例外。 如果回傳的任務中儲存了例外,該例外會在等待任務時拋出。 使用例外錯誤,例如 ArgumentException,仍會同步拋出。 關於儲存的例外,請參見由 Write(Byte[], Int32, Int32)拋出的例外。

適用於

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

來源:
Stream.cs
來源:
Stream.cs
來源:
Stream.cs
來源:
Stream.cs
來源:
Stream.cs

非同步地將一串位元組寫入目前串流,依照寫入位元組數將該串流中的當前位置前進,並監控取消請求。

public:
 virtual System::Threading::Tasks::Task ^ WriteAsync(cli::array <System::Byte> ^ buffer, int offset, int count, System::Threading::CancellationToken cancellationToken);
public virtual System.Threading.Tasks.Task WriteAsync(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
[<System.Runtime.InteropServices.ComVisible(false)>]
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
Public Overridable Function WriteAsync (buffer As Byte(), offset As Integer, count As Integer, cancellationToken As CancellationToken) As Task

參數

buffer
Byte[]

用來寫入資料的緩衝區。

offset
Int32

以零為基礎的位元組偏移量 buffer ,從中開始將位元組複製到串流。

count
Int32

最大可寫入位元組數。

cancellationToken
CancellationToken

用來監控取消申請的代幣。 預設值為 None

傳回

一個代表非同步寫入操作的任務。

屬性

例外狀況

buffernull

offsetcount 是陰性。

count的和offset大於緩衝區長度。

該課程不支援寫作。

溪流已被處理。

該串流目前正被先前的寫入操作使用。

取消令牌也被取消了。 此例外會儲存在回傳的任務中。

備註

WriteAsync 方法能讓你執行資源密集型的 I/O 操作,而不阻塞主執行緒。 在 Windows 8.x 市集應用程式或傳統型應用程式中,此效能考慮特別重要,其中耗時的串流作業可能會封鎖 UI 線程,並讓應用程式看起來好像無法運作。 非同步方法與 asyncawait 關鍵字搭配使用 Visual Basic 和 C# 中。

利用該 CanWrite 屬性判斷目前實例是否支援寫入。

若操作在完成前被取消,回傳的任務會包含 CanceledStatus 屬性的值。

舉例來說,請參考過載。WriteAsync(Byte[], Int32, Int32)

此方法在任務中儲存所有非使用例外,該方法的同步對應程式可拋出這些例外。 如果回傳的任務中儲存了例外,該例外會在等待任務時拋出。 使用例外錯誤,例如 ArgumentException,仍會同步拋出。 關於儲存的例外,請參見由 Write(Byte[], Int32, Int32)拋出的例外。

另請參閱

適用於