/// <summary> /// Set the ByteBuffer's FlushAsyncArgs to invoke FlushAsync() /// </summary> /// <returns></returns> internal AsyncCompletionResult FlushAsync() { if (this.position <= 0) { return(AsyncCompletionResult.Completed); } Fx.Assert(this.FlushAsyncArgs != null, "FlushAsyncArgs not set."); if (flushCallback == null) { flushCallback = new AsyncCallback(OnAsyncFlush); } int bytesToWrite = this.position; this.SetWritePending(); this.position = 0; if (TD.BufferedAsyncWriteStartIsEnabled()) { TD.BufferedAsyncWriteStart(this.parent.EventTraceActivity, this.GetHashCode(), bytesToWrite); } IAsyncResult asyncResult = this.stream.BeginWrite(this.bytes, 0, bytesToWrite, flushCallback, this); if (asyncResult.CompletedSynchronously) { if (TD.BufferedAsyncWriteStopIsEnabled()) { TD.BufferedAsyncWriteStop(this.parent.EventTraceActivity); } this.stream.EndWrite(asyncResult); this.ResetWritePending(); return(AsyncCompletionResult.Completed); } return(AsyncCompletionResult.Queued); }