-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathDownloadPogressExample.cs
56 lines (53 loc) · 1.49 KB
/
DownloadPogressExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
namespace ShellProgressBar.Example.Examples
{
public class DownloadProgressExample : ExampleBase
{
protected override Task StartAsync()
{
var files = new string[]
{
"https://github.com/Mpdreamz/shellprogressbar/archive/4.3.0.zip",
"https://github.com/Mpdreamz/shellprogressbar/archive/4.2.0.zip",
"https://github.com/Mpdreamz/shellprogressbar/archive/4.1.0.zip",
"https://github.com/Mpdreamz/shellprogressbar/archive/4.0.0.zip",
};
var childOptions = new ProgressBarOptions
{
ForegroundColor = ConsoleColor.Yellow,
ProgressCharacter = '\u2593'
};
using var pbar = new ProgressBar(files.Length, "downloading");
foreach (var (file, i) in files.Select((f, i) => (f, i)))
{
byte[] data = null;
using var child = pbar.Spawn(100, "page: " + i, childOptions);
try
{
#pragma warning disable CS0618
#pragma warning disable SYSLIB0014
using var client = new WebClient();
#pragma warning restore CS0618
#pragma warning restore SYSLIB0014
client.DownloadProgressChanged += (o, args) => child.Tick(args.ProgressPercentage);
client.DownloadDataCompleted += (o, args) => data = args.Result;
client.DownloadDataAsync(new Uri(file));
while (client.IsBusy)
{
Thread.Sleep(1);
}
pbar.Tick();
}
catch (WebException error)
{
pbar.WriteLine(error.Message);
}
}
return Task.CompletedTask;
}
}
}