2

I'm trying to download a list of files (using reactive extensions), maintaining maximum TWO parallel downloads at a time.

So far I didn't come across a working sample.

The closest thing so far is :

How can I limit Parallel.ForEach?

But I want to do the same that via Rx.net Any help would be really appreciated.

2
  • Are you using Rx because list is dynamic? Commented Jul 9, 2014 at 12:36
  • Not necessarily. It'd be list of n URLs or Action (sync methods i.e the download activity) Commented Jul 9, 2014 at 16:39

1 Answer 1

3

Observable.Merge takes a maxConcurrent argument which lets you limit how many parallel subscriptions are maintained. So you can do something like this:

public class DownloadResult { /*...*/ } public Task<DownloadResult> DownloadFileAsync(string path) { /*...*/ } public IObservable<DownloadResult> DownloadFiles(int maxConcurrent, string[] paths) { return paths .Select(path => Observable.FromAsync(() => DownloadFileAsync(path))) .Merge(maxConcurrent: maxConcurrent); } 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help. I ran your script with linqpad but it won't yield the desired result. Can you please comment on this : pastebin.com/5WBQXQv5
yes your DownloadAsync isnt really async. Never new a Task. Use Task.Run instead. Or declare your method async and get rid of the Task construction and use await Task.Delay(1000); instead of Sleep

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.