public void UploadFile(string source, IResource destinationResource) { destinationResource.TimeOut = System.Threading.Timeout.Infinite; this.buttonReady.Enabled = false; this.labelFileDownload.Text = "Datei (" + destinationResource.DisplayName + ") wird hochgeladen: "; this._downloadInProgress = true; FileInfo file = new FileInfo(source); this._worker = new BackgroundWorker(); this._worker.WorkerReportsProgress = true; this._worker.WorkerSupportsCancellation = true; this._worker.DoWork += new DoWorkEventHandler((object sender, DoWorkEventArgs e) => { Console.WriteLine("BackgroundWorker gestartet"); try { using(Stream webStream = destinationResource.GetWriteStream("application/octet-stream", file.Length)) { int bufSize = 1024; // 1Kb byte[] buffer = new byte[bufSize]; int bytesRead = 0; long totalBytesRead = 0; DateTime begin = DateTime.Now; using(FileStream fileStream = file.OpenRead()) { do { if (this._worker.CancellationPending) { e.Cancel = true; Console.WriteLine("BackgroundWorker wird abgebrochen!"); break; } bytesRead = fileStream.Read(buffer, 0, bufSize); if (bytesRead > 0) { try { webStream.Write(buffer, 0, bytesRead); totalBytesRead += bytesRead; if (DateTime.Now.Subtract(begin).TotalSeconds >= 1) { int ProgressPercentage = (int)(totalBytesRead * 100 / file.Length); this._worker.ReportProgress(ProgressPercentage, totalBytesRead); begin = DateTime.Now; } } catch(Exception writeException) { e.Cancel = true; Console.WriteLine("Verbindung wurde unerwartet unterbrochen! (" + totalBytesRead + " bytes)"); MessageBox.Show(writeException.Message); break; } } } while (bytesRead > 0); } string result = ""; if (webStream.CanRead) { byte[] buffer2 = new byte[8192]; int bytesRead2 = 0; do { bytesRead2 = webStream.Read(buffer2, 0, buffer2.Length); if (bytesRead2 > 0) { result += Encoding.UTF8.GetString(buffer2, 0, bytesRead2); } } while (bytesRead2 > 0); } if (result != String.Empty) { Console.WriteLine(result); } } } catch(Exception exc) { e.Cancel = true; MessageBox.Show(exc.Message); } } ); this._worker.ProgressChanged += new ProgressChangedEventHandler((object sender, ProgressChangedEventArgs e) => { int ProgressPercentage = Convert.ToInt32((long)e.UserState * 100.0 / file.Length); this.labelFileDownload.Text = String.Format("Datei ({0}) wird hochgeladen: {1}/{2}", destinationResource.DisplayName, this.GetBestSizeFormat((long)e.UserState), this.GetBestSizeFormat(file.Length)); this.labelPercentage.Text = ProgressPercentage.ToString() + "%"; progressBarFileDownload.Value = ProgressPercentage; } ); this._worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler((object sender, RunWorkerCompletedEventArgs e) => { Console.WriteLine("BackgroundWorker fertig!"); if (!e.Cancelled) { progressBarFileDownload.Value = 100; this.labelFileDownload.Text = String.Format("Datei ({0}) wird hochgeladen: {1}/{2}", destinationResource.DisplayName, this.GetBestSizeFormat(file.Length), this.GetBestSizeFormat(file.Length)); this.labelPercentage.Text = "100%"; } this._downloadInProgress = false; this.buttonReady.Enabled = true; } ); this._worker.RunWorkerAsync(); }