private void CopyStreamAsync(Stream input, Stream output, bool flushInput, bool flushOutput, StreamCopyCompletedDelegate completed)
        {
            byte[] buffer = new byte[1024 * 4];
            var asyncOp = System.ComponentModel.AsyncOperationManager.CreateOperation(null);

            Action<Exception> done = e =>
            {
                if (completed != null) asyncOp.Post(delegate
                    {
                        completed(input, output, e);
                    }, null);
            };

            AsyncCallback rc = null;
            rc = readResult =>
            {
                try
                {
                    int read = input.EndRead(readResult);
                    if (read > 0)
                    {
                        if (flushInput) input.Flush();
                        output.BeginWrite(buffer, 0, read, writeResult =>
                        {
                            try
                            {
                                output.EndWrite(writeResult);
                                if (flushOutput) output.Flush();
                                input.BeginRead(buffer, 0, buffer.Length, rc, null);
                            }
                            catch (Exception exc) { done(exc); }
                        }, null);
                    }
                    else done(null);
                }
                catch (Exception exc) { done(exc); }
            };

            input.BeginRead(buffer, 0, buffer.Length, rc, null);
        }
Exemple #2
0
        private void CopyStreamAsync(Stream input, Stream output, bool flushInput, bool flushOutput, StreamCopyCompletedDelegate completed)
        {
            byte[] buffer  = new byte[1024 * 4];
            var    asyncOp = System.ComponentModel.AsyncOperationManager.CreateOperation(null);

            Action <Exception> done = e =>
            {
                if (completed != null)
                {
                    asyncOp.Post(delegate
                    {
                        completed(input, output, e);
                    }, null);
                }
            };

            AsyncCallback rc = null;

            rc = readResult =>
            {
                try
                {
                    int read = input.EndRead(readResult);
                    if (read > 0)
                    {
                        if (flushInput)
                        {
                            input.Flush();
                        }
                        output.BeginWrite(buffer, 0, read, writeResult =>
                        {
                            try
                            {
                                output.EndWrite(writeResult);
                                if (flushOutput)
                                {
                                    output.Flush();
                                }
                                input.BeginRead(buffer, 0, buffer.Length, rc, null);
                            }
                            catch (Exception exc) { done(exc); }
                        }, null);
                    }
                    else
                    {
                        done(null);
                    }
                }
                catch (Exception exc) { done(exc); }
            };

            input.BeginRead(buffer, 0, buffer.Length, rc, null);
        }