Skip to content

Instantly share code, notes, and snippets.

@sangheestyle
Last active September 25, 2018 21:45
Show Gist options
  • Save sangheestyle/84c21db2ddf354349f59569ab102dfa6 to your computer and use it in GitHub Desktop.
Save sangheestyle/84c21db2ddf354349f59569ab102dfa6 to your computer and use it in GitHub Desktop.
Run async tasks concurrently on Rx with maximum concurrent level
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