Skip to content

Instantly share code, notes, and snippets.

@dhilip89
Created December 17, 2019 07:30
Show Gist options
  • Save dhilip89/fafa7b5918a9dd004d4ba1f5e70b9d69 to your computer and use it in GitHub Desktop.
Save dhilip89/fafa7b5918a9dd004d4ba1f5e70b9d69 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
namespace DownloadTaskDemo
{
public class DownloadTaskManager
{
private List<DownloadTask> tasks = new List<DownloadTask>();
public IEnumerable<DownloadTask> Tasks { get { return tasks.AsEnumerable(); } }
public double Progress { get; private set; }
public event EventHandler TasksProgressChanged;
public void Add(DownloadTask task)
{
task.ProgressChanged += Task_ProgressChanged;
tasks.Add(task);
}
private void Task_ProgressChanged(object sender, EventArgs e)
{
Progress = tasks.Average(x => x.Progress);
TasksProgressChanged?.Invoke(this, e);
}
public void StartAllTasks()
{
tasks.ForEach(x => x.Start());
}
}
public class DownloadTask : IDisposable
{
private readonly WebClient webClient;
private bool disposedValue = false;
public Uri Uri { get; private set; }
public string FilePath { get; private set; }
public double Progress { get; private set; }
public long BytesReceived { get; private set; }
public long TotalBytesToReceive { get; private set; }
public event EventHandler ProgressChanged;
public DownloadTask(Uri uri, string filePath)
{
Uri = uri;
FilePath = filePath;
webClient = new WebClient();
webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
}
private void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Progress = (double)e.ProgressPercentage / 100;
BytesReceived = e.BytesReceived;
TotalBytesToReceive = e.TotalBytesToReceive;
ProgressChanged?.Invoke(this, e);
}
public void Start()
{
webClient.DownloadFileAsync(Uri, FilePath);
}
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
webClient.Dispose();
}
disposedValue = true;
}
}
public void Dispose()
{
Dispose(true);
}
}
//=============================================================================================
// IMPORTANT: Just for demo purpose, please enhance before using it in production environment.
//=============================================================================================
class Program
{
static DownloadTaskManager downloadTaskManager = new DownloadTaskManager();
static Timer timer = new Timer(Timer_Elapsed);
static void Main(string[] args)
{
downloadTaskManager.Add(new DownloadTask(new Uri("http://old-releases.ubuntu.com/releases/18.10/ubuntu-18.10-desktop-amd64.iso"), "ubuntu-18.10-desktop-amd64.iso"));
downloadTaskManager.Add(new DownloadTask(new Uri("http://old-releases.ubuntu.com/releases/18.10/ubuntu-18.10-live-server-amd64.iso"), "ubuntu-18.10-live-server-amd64.iso"));
downloadTaskManager.Add(new DownloadTask(new Uri("http://old-releases.ubuntu.com/releases/18.10/ubuntu-18.10-server-ppc64el.iso"), "ubuntu-18.10-server-ppc64el.iso"));
downloadTaskManager.Add(new DownloadTask(new Uri("http://old-releases.ubuntu.com/releases/18.10/ubuntu-18.10-server-arm64.iso"), "ubuntu-18.10-server-arm64.iso"));
downloadTaskManager.Add(new DownloadTask(new Uri("http://old-releases.ubuntu.com/releases/18.10/ubuntu-18.10-server-amd64.iso"), "ubuntu-18.10-server-amd64.iso"));
downloadTaskManager.StartAllTasks();
timer.Change(0, Timeout.Infinite);
while (Console.ReadKey().KeyChar != 'q') ;
}
static void Timer_Elapsed(object state)
{
Console.Clear();
Console.WriteLine("+----------------------------------------+---------------+");
Console.WriteLine("| File | Progress (%) |");
Console.WriteLine("+----------------------------------------+---------------+");
foreach (var task in downloadTaskManager.Tasks)
{
Console.WriteLine("| {0,-39}| {1,-14}|", task.FilePath, (int)(task.Progress * 100));
}
Console.WriteLine("+----------------------------------------+---------------+");
Console.WriteLine("| All Progress | {0,-14}|", (int)(downloadTaskManager.Progress * 100));
Console.WriteLine("+----------------------------------------+---------------+");
timer.Change(500, Timeout.Infinite);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment