Beispiel #1
0
            private bool OnProgressError(StreamCopyProgressErrorEventArgs e)
            {
                EventHandler <StreamCopyProgressErrorEventArgs> handler = this.ProgressError;

                if (handler != null)
                {
                    handler(this, e);
                    return(true);
                }

                return(false);
            }
Beispiel #2
0
            public void Start()
            {
                lock (this._SYNC)
                {
                    this.IsCancelling = false;
                    this.Failed       = false;
                    this.Canceled     = false;

                    this.Errors = null;

                    List <Exception> occuredExceptions = new List <Exception>();

                    try
                    {
                        this.IsRunning = true;

                        this.RaiseEventHandler(this.Started);
                        this.OnProgress(new StreamCopyProgressEventArgs(this._COPIER,
                                                                        0, 0));

                        byte[] buffer = new byte[this._COPIER._BUFFER_SIZE];

                        long totalCopied = 0;

                        while (true)
                        {
                            int bytesRead = 0;
                            try
                            {
                                bytesRead = this._COPIER.Source.Read(buffer, 0, buffer.Length);
                                if (bytesRead < 1)
                                {
                                    break;
                                }

                                if (this.IsCancelling)
                                {
                                    break;
                                }

                                StreamCopyBeforeWriteEventArgs bwe = new StreamCopyBeforeWriteEventArgs(CollectionHelper.Take(buffer, bytesRead),
                                                                                                        this._COPIER,
                                                                                                        totalCopied);
                                this.OnBeforeWrite(bwe);

                                if (bwe.Cancel)
                                {
                                    this.IsCancelling = true;
                                    break;
                                }

                                if (bwe.Skip)
                                {
                                    // skip write operation
                                    continue;
                                }

                                int bytesCopied = 0;

                                byte[] dataToWrite = CollectionHelper.AsArray(bwe.Data);
                                if (dataToWrite != null)
                                {
                                    if (dataToWrite.Length > 0)
                                    {
                                        bytesCopied = dataToWrite.Length;

                                        this._COPIER.Destination.Write(dataToWrite, 0, bytesCopied);
                                        totalCopied += bytesCopied;
                                    }
                                }

                                StreamCopyProgressEventArgs e = new StreamCopyProgressEventArgs(this._COPIER,
                                                                                                bytesCopied, totalCopied);
                                this.OnProgress(e);

                                if (e.Cancel)
                                {
                                    this.IsCancelling = true;
                                    break;
                                }
                            }
                            catch (Exception ex)
                            {
                                StreamCopyProgressErrorEventArgs e = new StreamCopyProgressErrorEventArgs(ex,
                                                                                                          this._COPIER,
                                                                                                          bytesRead, totalCopied);
                                e.Cancel    = true;
                                e.LogErrors = true;

                                this.OnProgressError(e);

                                if (e.LogErrors)
                                {
                                    occuredExceptions.Add(e.Errors);
                                }

                                if (e.Handled == false)
                                {
                                    // (re-)throw
                                    throw e.Errors;
                                }

                                if (e.Cancel)
                                {
                                    this.IsCancelling = true;
                                    break;
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        occuredExceptions.Add(ex);

                        this.Failed = true;
                    }
                    finally
                    {
                        this.Errors = new ReadOnlyCollection <Exception>(occuredExceptions);

                        if (this.IsCancelling)
                        {
                            this.Canceled = true;
                        }
                        this.IsCancelling = false;

                        this.IsRunning = false;
                        this.RaiseEventHandler(this.Completed);
                    }
                }
            }