public void Finished() { DownloadState = DownloadStates.Finished; ProgressPercentage = 100; //100% DownloadedSize = FileSize; AssociatedDownloadControl.UpdateProgress(); }
// Pause download public void Pause() { if (DownloadState == DownloadStates.InProgress) { DownloadState = DownloadStates.Paused; AssociatedDownloadControl.UpdateProgress(); } }
public void Cancel() { if (DownloadState == DownloadStates.InProgress) { DownloadState = DownloadStates.Canceled; ResetProperties(); AssociatedDownloadControl.UpdateProgress(); } }
// Restart download public void Restart() { if (DownloadState == DownloadStates.Canceled || DownloadState == DownloadStates.Finished) { ResetProperties(); if (File.Exists(DownloadTo)) { File.Delete(DownloadTo); } DownloadState = DownloadStates.PreStart; AssociatedDownloadControl.UpdateProgress(); Task.Run(() => AssociatedDownloadControl.AssociatedView.Dispatcher.Invoke(() => { Start(); })); } }
public void Downloading() { // Download file bytes until the download is paused, canceled or completed while (DownloadState != DownloadStates.Canceled && DownloadState != DownloadStates.Finished && DownloadState != DownloadStates.Paused) { try { // Read data from the response stream and write it to the buffer bytesSize = binaryReader.Read(downloadBuffer, 0, (int)FileSize); if (bytesSize > 0) { Array.Copy(downloadBuffer, 0, totalDownloadBuffer, lastBytesSize, bytesSize); //copying the content to the full buffer _downloadBytes.Add(bytesSize); DownloadedSize = _downloadBytes.Sum(); ProgressPercentage = (int)(((float)DownloadedSize / (float)FileSize) * 100); AssociatedDownloadControl.UpdateProgress(); Array.Clear(downloadBuffer, 0, bytesSize); //clearing for future content if (DownloadedSize >= FileSize) { Finished(); } } else { Finished(); } lastBytesSize += bytesSize; } catch (Exception) { break; } } if (DownloadState == DownloadStates.Finished) { File.WriteAllBytes(DownloadTo, totalDownloadBuffer); Stop(); } }