Exemple #1
0
        /// <summary>
        /// Copies a stream asynchronously. This method is useful for copying data to a network stream because the network stream
        /// will actually buffer at the hardware level and the calling thread will not have to block. when the asynchronous copy
        /// has complete, the delegate specified by the endHandler argument will be called to complete the operation
        /// </summary>
        /// <param name="inSource">The stream to copy from</param>
        /// <param name="inDestination">The stream to copy to. Flush() will be called when the copy is complete</param>
        /// <param name="endHandler">The delegate to call when the async copy operation is complete</param>
        /// <param name="state">The state, if any that the delegate would like returned to it</param>
        public static void BeginCopyStream(Stream inSource,
                                           Stream inDestination,
                                           int bufferSize,
                                           EduAsyncHandler endHandler,
                                           object state)
        {
            AsyncStreamState streamState =
                new AsyncStreamState(inSource, inDestination, bufferSize, endHandler, state);

            DoAsyncCopyStep(streamState);
        }
Exemple #2
0
        private static void EndCopyStream(IAsyncResult result)
        {
            AsyncStreamState state = (AsyncStreamState)result.AsyncState;

            try {
                state.fDestinationStream.EndWrite(result);
            }
            catch (Exception ex) {
                state.SetException(ex);
                state.AsyncHandler(state);
            }
            DoAsyncCopyStep(state);
        }
Exemple #3
0
        private static void DoAsyncCopyStep(AsyncStreamState state)
        {
            int aBytesRead = state.fSourceStream.Read(state.fBuffer, 0, state.fBufferSize);

            if (aBytesRead > 0)
            {
                state.fDestinationStream.BeginWrite
                    (state.fBuffer, 0, aBytesRead, sEndWriteHandler, state);
            }
            else
            {
                state.fDestinationStream.Flush();
                state.AsyncHandler(state);
            }
        }
Exemple #4
0
 private static void DoAsyncCopyStep( AsyncStreamState state )
 {
     int aBytesRead = state.fSourceStream.Read( state.fBuffer, 0, state.fBufferSize );
     if ( aBytesRead > 0 ) {
         state.fDestinationStream.BeginWrite
             ( state.fBuffer, 0, aBytesRead, sEndWriteHandler, state );
     }
     else {
         state.fDestinationStream.Flush();
         state.AsyncHandler( state );
     }
 }
Exemple #5
0
 /// <summary>
 /// Copies a stream asynchronously. This method is useful for copying data to a network stream because the network stream 
 /// will actually buffer at the hardware level and the calling thread will not have to block. when the asynchronous copy
 /// has complete, the delegate specified by the endHandler argument will be called to complete the operation
 /// </summary>
 /// <param name="inSource">The stream to copy from</param>
 /// <param name="inDestination">The stream to copy to. Flush() will be called when the copy is complete</param>
 /// <param name="endHandler">The delegate to call when the async copy operation is complete</param>
 /// <param name="state">The state, if any that the delegate would like returned to it</param>
 public static void BeginCopyStream( Stream inSource,
                                     Stream inDestination,
                                     int bufferSize,
                                     EduAsyncHandler endHandler,
                                     object state )
 {
     AsyncStreamState streamState =
         new AsyncStreamState( inSource, inDestination, bufferSize, endHandler, state );
     DoAsyncCopyStep( streamState );
 }