Last active
September 25, 2018 21:45
-
-
Save sangheestyle/84c21db2ddf354349f59569ab102dfa6 to your computer and use it in GitHub Desktop.
Run async tasks concurrently on Rx with maximum concurrent level
This file contains hidden or 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
using System; | |
using System.Threading.Tasks; | |
using System.Reactive.Linq; | |
namespace rxconcurrent | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var maxConcurrentLevel = 2; | |
IObservable<long> ticks = Observable.Interval(TimeSpan.FromSeconds(0.1)); | |
ticks.Select(x => Observable.FromAsync(async () => | |
{ | |
await Task.Delay(1000); | |
return x; | |
})) | |
.Merge(maxConcurrentLevel) | |
.Subscribe(x => Console.WriteLine($"Tick! {x}")); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment