Created
December 8, 2019 14:23
-
-
Save valeriob/d264c82f20cd6af2dd6f446728e46f8e to your computer and use it in GitHub Desktop.
RxExtensions Observable.FromAsync
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.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