Export (0) Print
Expand All

FileStream::BeginWrite Method

Begins an asynchronous write.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

[HostProtectionAttribute(SecurityAction::LinkDemand, ExternalThreading = true)]
public:
virtual IAsyncResult^ BeginWrite(
	array<unsigned char>^ array, 
	int offset, 
	int numBytes, 
	AsyncCallback^ userCallback, 
	Object^ stateObject
) override

Parameters

array
Type: array<System::Byte>

The buffer containing data to write to the current stream.

offset
Type: System::Int32

The zero-based byte offset in array at which to begin copying bytes to the current stream.

numBytes
Type: System::Int32

The maximum number of bytes to write.

userCallback
Type: System::AsyncCallback

The method to be called when the asynchronous write operation is completed.

stateObject
Type: System::Object

A user-provided object that distinguishes this particular asynchronous write request from other requests.

Return Value

Type: System::IAsyncResult
An object that references the asynchronous write.

ExceptionCondition
ArgumentException

array length minus offset is less than numBytes.

ArgumentNullException

array is nullptr.

ArgumentOutOfRangeException

offset or numBytes is negative.

NotSupportedException

The stream does not support writing.

ObjectDisposedException

The stream is closed.

IOException

An I/O error occurs.

NoteNote:

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

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.

Multiple simultaneous asynchronous requests render the request completion order uncertain.

Windows Mobile for Pocket PC, Windows Mobile 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.

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 );
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft