complete-reference-vb_net_56
complete-reference-vb_net_56
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.
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.
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