Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Main()
- {
- var urls = new[]{ "url1", "url2", "url3", "url4" };
- DownloadFiles(2,urls).Subscribe();
- Thread.Sleep(5000); // delay program quitting to observe threads output
- }
- public class DownloadResult { /*...*/ }
- public Task<DownloadResult> DownloadFileAsync(string path)
- {
- return new Task<DownloadResult>(()=>
- {
- ("Started : " + path).Dump();
- Thread.Sleep(1000); // the download activity (i.e sync method) ... WebClient.DownloadFile
- ("Finished : " + path).Dump();
- return new DownloadResult();
- });
- }
- public IObservable<DownloadResult> DownloadFiles(int maxConcurrent, string[] paths)
- {
- return paths
- .Select(path => Observable.FromAsync(() => DownloadFileAsync(path)))
- .Merge(maxConcurrent: maxConcurrent);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement