FileStream.BeginWrite Method
Assembly: mscorlib (in mscorlib.dll)
public override IAsyncResult BeginWrite ( byte[] array, int offset, int numBytes, AsyncCallback userCallback, Object stateObject )
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
Not applicable.
Parameters
- array
The buffer containing data to write to the current stream.
- offset
The zero-based byte offset in array at which to begin copying bytes to the current stream.
- numBytes
The maximum number of bytes to write.
- userCallback
The method to be called when the asynchronous write operation is completed.
- stateObject
A user-provided object that distinguishes this particular asynchronous write request from other requests.
Return Value
An IAsyncResult that references the asynchronous write.EndWrite must be called exactly once on every IAsyncResult from BeginWrite. EndWrite will block until the I/O operation has completed.
This method overrides BeginWrite.
FileStream provides two different modes of operation: synchronous I/O and asynchronous I/O. While either can be used, the underlying operating system resources might allow access in only one of these modes. By default, FileStream opens the operating system handle synchronously. In Windows, this slows down asynchronous methods. If asynchronous methods are used, use the FileStream(String,FileMode,FileAccess,FileShare,Int32,Boolean) constructor.
If a stream is closed or you pass an invalid argument, exceptions are thrown immediately from BeginWrite. Errors that occur during an asynchronous write request, such as a disk failure during the IO request, occur on the thread pool thread and become visible upon a call to EndWrite.
Note: |
|---|
| On Windows, all I/O operations smaller than 64 KB will complete synchronously for better performance. Asynchronous I/O might hinder performance for buffer sizes smaller than 64 KB. |
Multiple simultaneous asynchronous requests render the request completion order uncertain.
For a list of common I/O tasks, see Common I/O Tasks.
Windows Mobile 2003 for Pocket PC, Windows Mobile 2003 for Smartphone, Windows CE Platform Note: In .NET Compact Framework applications, this method is supported, but it is reserved for future use. Using this method will raise a NotSupportedException until it becomes available in a future release.
This code example is part of a larger example provided for the FileStream(String,FileMode,FileAccess,FileShare,Int32,Boolean) constructor.
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); }
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 Server 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
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.
Note: