void DataStreamRead_End(IAsyncResult ar) { TransStateObjectStream stateObj = (TransStateObjectStream)ar.AsyncState; try { stateObj.UpdateContext(); int num = DataStream.EndRead(ar); DTPStream userStream = (DTPStream)stateObj.UserStream; if (num > 0) { long require = userStream.AvailableSpace; if (num > require) { num = (int)require; } stateObj.Transfered += num; OnDataTransfered(_workBuffer, num); //write received data to user stream userStream.BeginWrite(_workBuffer, 0, num, new AsyncCallback(this.UserStreamWrite_End), stateObj); } else { userStream.Flush(); OnCompleted(); stateObj.SetCompleted(); } } catch (Exception e) { stateObj.Exception = e; stateObj.SetCompleted(); } catch { NSTrace.WriteLineError("Non-CLS exception at: " + Environment.StackTrace); throw; } }