-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add download data example to illustrate #56
- Loading branch information
Showing
4 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/ShellProgressBar.Example/Examples/DownloadPogressExample.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Threading; | ||
|
||
namespace ShellProgressBar.Example.Examples | ||
{ | ||
public class DownloadProgressExample : ExampleBase | ||
{ | ||
protected override void Start() | ||
{ | ||
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 | ||
{ | ||
using var client = new WebClient(); | ||
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); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters