FileStream.BeginWrite メソッド
アセンブリ: 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
戻り値
非同期の書き込みを参照する IAsyncResult。


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 タスクの例を次の表に示します。
File.AppendText FileInfo.AppendText | |
File.Move FileInfo.MoveTo | |
File.Copy FileInfo.CopyTo | |
FileInfo.Length | |
File.GetAttributes | |
File.SetAttributes | |
Directory.CreateDirectory Directory.CreateDirectory |

次のコード例は、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

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- FileStream.BeginWrite メソッドのページへのリンク