public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
CombinationStreamAsyncResult asyncResult = new CombinationStreamAsyncResult(state);
if (count > 0)
{
int buffPostion = offset;
AsyncCallback rc = null;
rc = readresult =>
{
try
{
int bytesRead = _currentStream.EndRead(readresult);
asyncResult.BytesRead += bytesRead;
buffPostion += bytesRead;
_postion += bytesRead;
if (bytesRead <= count)
count -= bytesRead;
if (count > 0)
{
if (_currentStreamIndex >= _streams.Count)
{
// done
asyncResult.CompletedSynchronously = false;
asyncResult.SetAsyncWaitHandle();
asyncResult.IsCompleted = true;
callback(asyncResult);
}
else
{
_currentStream = _streams[_currentStreamIndex++];
_currentStream.BeginRead(buffer, buffPostion, count, rc, readresult.AsyncState);
}
}
else
{
// done
asyncResult.CompletedSynchronously = false;
asyncResult.SetAsyncWaitHandle();
asyncResult.IsCompleted = true;
callback(asyncResult);
}
}
catch (Exception ex)
{
// done
asyncResult.Exception = ex;
asyncResult.CompletedSynchronously = false;
asyncResult.SetAsyncWaitHandle();
asyncResult.IsCompleted = true;
callback(asyncResult);
}
};
_currentStream.BeginRead(buffer, buffPostion, count, rc, state);
}
else
{
// done
asyncResult.CompletedSynchronously = true;
asyncResult.SetAsyncWaitHandle();
asyncResult.IsCompleted = true;
callback(asyncResult);
}
return asyncResult;
}