0% found this document useful (0 votes)
13 views

complete-reference-vb_net_56

Uploaded by

khalid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

complete-reference-vb_net_56

Uploaded by

khalid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

FileStream

Both sets of methods will work in either mode; however, the mode will affect the performance of these
methods. FileStream defaults to opening files synchronously, but the constructor is overloaded and provides a
version of New to open files asynchronously.

While either can be used, the underlying file system determines which resources might allow access in only
one of these modes. While FileStream opens the operating system handle synchronously, this impacts
asynchronous method calls, which are made independently of the file systems. To use asynchronous methods,
construct the object with a constructor that allows you to specify an isAsync argument.

The signature of this BeginRead method is as follows:

Overrides Public Function BeginRead(ByVal array() As Byte, _


ByVal offset As Integer, _
ByVal numBytes As Integer, _
ByVal userCallback As AsyncCallback, _
ByVal stateObject As Object _
) As IAsyncResult

The following list describes its parameters:

• array A buffer to read data into


• offset The byte offset in the array at which to begin reading
• numBytes The maximum number of bytes to read
• userCallback The method to be called when the asynchronous read operation is completed
• stateObject A user−provided object that distinguishes this particular asynchronous read request from
other requests

Before you call BeginRead, issue a condition check on the CanRead property. This will let you determine
whether your object has the all−clear to move into high gear. BeginRead will also choke on invalid
arguments and throws out exceptions immediately. Wrapping up the method calls in exception handling code
is thus critical. Exceptions are also raised during asynchronous read requests. For example, a read may fail if
the file reference dies through disk failure, corruption, or some other file catastrophe.

By default, streams smaller than 64KB complete synchronously for better performance. The additional effort
required for asynchronous I/O on such small streams negates the advantages of asynchronous I/O. Also, you
need to call EndRead with IAsyncResult to find out how many bytes were read.

Table 15−24 lists the possible exceptions that can result from good read requests that go bad.

Table 15−24: Exceptions That Can Be Generated on a BeginRead Operation

Exception Condition
ArgumentException The array variable's length minus offset is less than numBytes
ArgumentNullException The array variable references Nothing
ArgumentOutOfRangeException The offset or numBytes is negative
IOException An asynchronous read was attempted past the end of the file

540

You might also like