Skip to content

Instantly share code, notes, and snippets.

@valeriob
Created December 8, 2019 14:23
Show Gist options
  • Select an option

  • Save valeriob/d264c82f20cd6af2dd6f446728e46f8e to your computer and use it in GitHub Desktop.

Select an option

Save valeriob/d264c82f20cd6af2dd6f446728e46f8e to your computer and use it in GitHub Desktop.
RxExtensions Observable.FromAsync
using System;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading.Tasks;
namespace ConsoleApp5
{
class Program
{
static void Main(string[] args)
{
var sbj = new Subject<int>();
var subscription1 = sbj.Select(t => Observable.FromAsync(DoStuffAsync)).Concat()
.Do(r=>
{
Console.WriteLine($"{DateTime.Now.TimeOfDay.Seconds} Subscription 1 : {GetCurrentThread()}");
})
.Subscribe();
var subscription2 = sbj.Select(t => Observable.FromAsync(DoStuffAsync)).Concat()
.Do(r =>
{
Console.WriteLine($"{DateTime.Now.TimeOfDay.Seconds} Subscription 2 : {GetCurrentThread()}");
})
.Subscribe();
for (int i = 0; i < 10; i++)
{
sbj.OnNext(i);
Console.WriteLine($"{DateTime.Now.TimeOfDay.Seconds} Next {i} : {GetCurrentThread()}");
}
Console.ReadLine();
}
static async Task DoStuffAsync()
{
await Task.Delay(2000);
}
static int GetCurrentThread()
{
return System.Threading.Thread.CurrentThread.ManagedThreadId;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment