FileStream.BeginWrite メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > FileStream.BeginWrite メソッドの意味・解説 

FileStream.BeginWrite メソッド

非同期の書き込みを開始します。

名前空間: System.IO
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Overrides Function
 BeginWrite ( _
    array As Byte(), _
    offset As Integer, _
    numBytes As Integer, _
    userCallback As AsyncCallback, _
    stateObject As Object _
) As IAsyncResult
Dim instance As FileStream
Dim array As Byte()
Dim offset As Integer
Dim numBytes As Integer
Dim userCallback As AsyncCallback
Dim stateObject As Object
Dim returnValue As IAsyncResult

returnValue = instance.BeginWrite(array, offset, numBytes, userCallback, stateObject)
public override IAsyncResult BeginWrite (
    byte[] array,
    int offset,
    int numBytes,
    AsyncCallback userCallback,
    Object stateObject
)
public:
virtual IAsyncResult^ BeginWrite (
    array<unsigned char>^ array, 
    int offset, 
    int numBytes, 
    AsyncCallback^ userCallback, 
    Object^ stateObject
) override
public IAsyncResult BeginWrite (
    byte[] array, 
    int offset, 
    int numBytes, 
    AsyncCallback userCallback, 
    Object stateObject
)
public override function BeginWrite (
    array : byte[], 
    offset : int, 
    numBytes : int, 
    userCallback : AsyncCallback, 
    stateObject : Object
) : IAsyncResult

パラメータ

array

現在のストリームに書き込むデータを格納しているバッファ。

offset

現在のストリームへのバイトのコピーを開始する位置を示す array 内のバイト オフセット。インデックス番号は 0 から始まります。

numBytes

書き込む最大バイト数。

userCallback

非同期の書き込み操作が完了したときに呼び出されるメソッド。

stateObject

この特定の非同期書き込み要求を他の要求と区別するために使用するユーザー指定のオブジェクト。

戻り値
非同期の書き込みを参照する IAsyncResult。

例外例外
例外の種類条件

ArgumentException

array から offset を差し引いた値が numBytes より小さい値です。

ArgumentNullException

array が null 参照 (Visual Basic では Nothing) です。

ArgumentOutOfRangeException

offset または numBytes が負の値です。

NotSupportedException

ストリームが書き込みをサポートしません。

ObjectDisposedException

ストリームが閉じられました。

IOException

I/O エラーが発生しました。

解説解説

EndWrite は、BeginWrite からの各 IAsyncResult ごとに必ず一度呼び出す必要があります。EndWrite は I/O 操作が完了するまでブロックします。

このメソッドは、BeginWrite をオーバーライドします。

FileStream には、同期 I/O と非同期 I/O の 2 種類の操作モードがあります。どちらも使用できる場合に、基になるオペレーティング システム リソースはいずれかのモードでのアクセスだけを許可することがあります。既定では、FileStream は同期的にオペレーティング システム ハンドルを開きます。Windows では、これによって非同期メソッドの速度が遅くなります。非同期メソッドを使用する場合は、FileStream(String,FileMode,FileAccess,FileShare,Int32,Boolean) コンストラクタを使用してください。

ストリームが閉じている場合、または無効な引数を渡した場合は、BeginWrite からすぐに例外がスローされます。I/O 要求中のディスク障害など、非同期書き込みの要求中に発生したエラーは、スレッド プールのスレッドで発生し、EndWrite を呼び出したときに可視になります。

メモメモ

Windows では、64 KB 未満のすべての I/O 操作を同期的に実行した方がパフォーマンスが高くなります。非同期 I/O は、64 KB 未満のバッファ サイズでのパフォーマンスを低下させることがあります。

EndWrite は、読み取られたバイト数を検出するために、この IAsyncResult と共に呼び出す必要があります。

複数の非同期要求を同時に実行した場合、要求の完了順序は不定です。

その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。

使用例使用例

次のコード例は、FileStream(String,FileMode,FileAccess,FileShare,Int32,Boolean) コンストラクタの例の一部です。

Shared Sub Main()

    ' Create a synchronization object that gets 
    ' signaled when verification is complete.
    Dim manualEvent As New
 ManualResetEvent(False)

    ' Create random data to write to the file.
    Dim writeArray(100000) As Byte
    Dim randomGenerator As New
 Random()
    randomGenerator.NextBytes(writeArray)

    Dim fStream As New FileStream("Test#@@#.dat",
 _
        FileMode.Create, FileAccess.ReadWrite, _
        FileShare.None, 4096, True)

    ' Check that the FileStream was opened asynchronously.
    If fStream.IsAsync = True
        Console.WriteLine("fStream was opened asynchronously.")
    Else
        Console.WriteLine("fStream was not opened asynchronously.")
    End If

    ' Asynchronously write to the file.
    Dim asyncResult As IAsyncResult = fStream.BeginWrite(
 _
        writeArray, 0, writeArray.Length, _
        AddressOf EndWriteCallback , _
        New State(fStream, writeArray, manualEvent))

    ' Concurrently do other work and then wait
    ' for the data to be written and verified.
    manualEvent.WaitOne(5000, False)
End Sub
static void Main()
{
    // Create a synchronization object that gets 
    // signaled when verification is complete.
    ManualResetEvent manualEvent = new ManualResetEvent(false);

    // Create random data to write to the file.
    byte[] writeArray = new byte[100000];
    new Random().NextBytes(writeArray);

    FileStream fStream = 
        new FileStream("Test#@@#.dat", FileMode.Create,
 
        FileAccess.ReadWrite, FileShare.None, 4096, true);

    // Check that the FileStream was opened asynchronously.
    Console.WriteLine("fStream was {0}opened asynchronously.",
        fStream.IsAsync ? "" : "not ");

    // Asynchronously write to the file.
    IAsyncResult asyncResult = fStream.BeginWrite(
        writeArray, 0, writeArray.Length, 
        new AsyncCallback(EndWriteCallback), 
        new State(fStream, writeArray, manualEvent));

    // Concurrently do other work and then wait 
    // for the data to be written and verified.
    manualEvent.WaitOne(5000, false);
}
int main()
{
   
   // Create a synchronization object that gets 
   // signaled when verification is complete.
   ManualResetEvent^ manualEvent = gcnew ManualResetEvent( false
 );
   
   // Create the data to write to the file.
   array<Byte>^writeArray = gcnew array<Byte>(100000);
   (gcnew Random)->NextBytes( writeArray );
   FileStream^ fStream = gcnew FileStream(  "Test#@@#.dat",FileMode::Create,FileAccess::ReadWrite,FileShare::None,4096,true
 );
   
   // Check that the FileStream was opened asynchronously.
   Console::WriteLine( "fStream was {0}opened asynchronously.", fStream->IsAsync
 ? (String^)"" : "not " );
   
   // Asynchronously write to the file.
   IAsyncResult^ asyncResult = fStream->BeginWrite( writeArray, 0, writeArray->Length,
 gcnew AsyncCallback( &FStream::EndWriteCallback ), gcnew State( fStream,writeArray,manualEvent
 ) );
   
   // Concurrently do other work and then wait 
   // for the data to be written and verified.
   manualEvent->WaitOne( 5000, false );
}

public static void main(String[]
 args)
{
    // Create a synchronization object that gets 
    // signaled when verification is complete.
    ManualResetEvent manualEvent = new ManualResetEvent(false);

    // Create random data to write to the file.
    ubyte writeArray[] = new ubyte[100000];
    new Random().NextBytes(writeArray);

    FileStream fStream =  new FileStream("Test#@@#.dat",
 FileMode.Create,
        FileAccess.ReadWrite, FileShare.None, 4096, true);

    // Check that the FileStream was opened asynchronously.
    Console.WriteLine("fStream was {0}opened asynchronously.",
        (fStream.get_IsAsync()) ? "" : "not ");
    FStream classfStream = new FStream();

    // Asynchronously write to the file.
    IAsyncResult asyncResult = fStream.BeginWrite(writeArray, 0,
        writeArray.length, new AsyncCallback(EndWriteCallback)
,
        classfStream.new State(fStream, writeArray, manualEvent));

    // Concurrently do other work and then wait 
    // for the data to be written and verified.
    manualEvent.WaitOne(5000, false);
} //main
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

FileStream.BeginWrite メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



FileStream.BeginWrite メソッドのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS