Begins an asynchronous read operation..
[Visual Basic]
Overloads Public Function BeginRead( _
ByVal buffer As Byte(), _
ByVal offset As Int32, _
ByVal count As Int32, _
ByVal callback As AsyncCallback, _
ByVal state As Object _
) As IAsyncResult
[C#]
public IAsyncResult BeginRead(
byte[] buffer,
Int32 offset,
Int32 count,
AsyncCallback callback,
object state
);
Parameters
buffer
The buffer to read the data into.
offset
The byte offset in buffer at which to begin writing data read from the stream.
count
The maximum number of bytes to read.
callback
An optional asynchronous callback, to be called when the read is complete.
state
A user-provided object that distinguishes this particular asynchronous read request from other requests.
Return Value
An System.IAsyncResult that represents the asynchronous read, which could still be pending.
Remarks
Unlike Read method, calls to
BeginRead will not block on those streams. Pass the IAsyncResult
return value to the EndRead
method of the stream to determine how many bytes were read and to release
operating system resources used for reading. EndRead must be called once
for every call to BeginRead. You can do this either by using the same
code that called BeginRead or in a callback passed to BeginRead.
The current position in the stream is updated when operation completes. Multiple
simultaneous asynchronous requests render the request completion order
uncertain.
Use the CanRead property
to determine whether the current instance supports reading.
If a stream is closed or you pass an invalid argument, exceptions are thrown
immediately from BeginRead. Errors that occur during an asynchronous read
request, such as a disk failure during the I/O request, occur on the thread pool
thread and throw exceptions when calling EndRead.
Example of async read (C#) - read all of /etc/passwd on remote host, and dump to console:
WeOnlyDo.Client.SFTPStream sp;
byte[] buf;
void EndReadCallback(IAsyncResult ar)
{
int i = sp.EndRead(ar);
if (i > 0)
{
Console.Write(new String(System.Text.Encoding.ASCII.GetChars(buf, 0, i)));
sp.BeginRead(buf, 0, buf.Length, new AsyncCallback(EndReadCallback), null);
}
else
sp.Close();
}
private void Form1_Load(object sender, System.EventArgs e)
{
sp = new WeOnlyDo.Client.SFTPStream("your.host.com", "joe", "yourpass", "/etc/passwd", System.IO.FileMode.Open);
buf = new byte[32768];
sp.BeginRead(buf, 0, buf.Length, new AsyncCallback(EndReadCallback), null);
}
See Also
SFTPStream Class | SFTPStream Members | WeOnlyDo.Client Namespace