Пример #1
0
        public Int32 EndRead(IAsyncResult asyncResult)
#endif
        {
            if (asyncResult == null)
            {
                throw new ArgumentNullException(nameof(asyncResult));
            }

            Contract.EndContractBlock();

            EnsureNotDisposed();
            EnsureCanRead();

            StreamOperationAsyncResult streamAsyncResult = asyncResult as StreamOperationAsyncResult;

            if (streamAsyncResult == null)
            {
                throw new ArgumentException(SR.Argument_UnexpectedAsyncResult, nameof(asyncResult));
            }

            streamAsyncResult.Wait();

            try
            {
                // If the async result did NOT process the async IO operation in its completion handler (i.e. check for errors,
                // cache results etc), then we need to do that processing now. This is to allow blocking-over-async IO operations.
                // See the big comment in BeginRead for details.

                if (!streamAsyncResult.ProcessCompletedOperationInCallback)
                {
                    streamAsyncResult.ProcessCompletedOperation();
                }

                // Rethrow errors caught in the completion callback, if any:
                if (streamAsyncResult.HasError)
                {
                    streamAsyncResult.CloseStreamOperation();
                    streamAsyncResult.ThrowCachedError();
                }

                // Done:

                Int64 bytesCompleted = streamAsyncResult.BytesCompleted;
                Debug.Assert(bytesCompleted <= unchecked ((Int64)Int32.MaxValue));

                return((Int32)bytesCompleted);
            }
            finally
            {
                // Closing multiple times is Ok.
                streamAsyncResult.CloseStreamOperation();
            }
        }
Пример #2
0
        public void EndWrite(IAsyncResult asyncResult)
#endif
        {
            if (asyncResult == null)
            {
                throw new ArgumentNullException("asyncResult");
            }

            Contract.EndContractBlock();

            EnsureNotDisposed();
            EnsureCanWrite();

            StreamOperationAsyncResult streamAsyncResult = asyncResult as StreamOperationAsyncResult;

            if (streamAsyncResult == null)
            {
                throw new ArgumentException(SR.Argument_UnexpectedAsyncResult, "asyncResult");
            }

            streamAsyncResult.Wait();

            try
            {
                // If the async result did NOT process the async IO operation in its completion handler (i.e. check for errors,
                // cache results etc), then we need to do that processing now. This is to allow blocking-over-async IO operations.
                // See the big comment in BeginWrite for details.

                if (!streamAsyncResult.ProcessCompletedOperationInCallback)
                {
                    streamAsyncResult.ProcessCompletedOperation();
                }

                // Rethrow errors caught in the completion callback, if any:
                if (streamAsyncResult.HasError)
                {
                    streamAsyncResult.CloseStreamOperation();
                    streamAsyncResult.ThrowCachedError();
                }
            }
            finally
            {
                // Closing multiple times is Ok.
                streamAsyncResult.CloseStreamOperation();
            }
        }