(not yet) 101 Rx Samples

本文详细介绍RxJS中的异步背景操作、观察者操作符、限制操作符等核心概念,并通过实例展示如何实现异步代码执行、并行执行及事件观察等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(not yet) 101 Rx Samples

You!
Yes, you, the one who is still scratching their head trying to figure out this Rx thing.
As you learn and explore, please feel free add your own samples here, or tweak existing ones!
Anyone can (and should!) edit this page. (edit button is at the bottom right of each page)

Asynchronous Background Operations

Start - Run Code Asynchronously

var o = Observable.Start(() => { Console.WriteLine("Calculating..."); Thread.Sleep(3000); Console.WriteLine("Done."); });
o.First();   // subscribe and wait for completion of background operation

Run a method asynchronously on demand

Execute a long-running method asynchronously. The method does not start running until there is a subscriber. The method is started every time the observable is created and subscribed, so there could be more than one running at once.

// Synchronous operation
public DataType DoLongRunningOperation(string param)
{
    ...
}

public IObservable<DataType> LongRunningOperationAsync(string param)
{
    return Observable.CreateWithDisposable<DataType>(
        o => Observable.ToAsync(DoLongRunningOperation)(param).Subscribe(o)
    );
}

ForkJoin - Parallel Execution

var o = Observable.ForkJoin(
    Observable.Start(() => { Console.WriteLine("Executing 1st on Thread: {0}", Thread.CurrentThread.ManagedThreadId); return "Result A"; }),
    Observable.Start(() => { Console.WriteLine("Executing 2nd on Thread: {0}", Thread.CurrentThread.ManagedThreadId); return "Result B"; }),
    Observable.Start(() => { Console.WriteLine("Executing 3rd on Thread: {0}", Thread.CurrentThread.ManagedThreadId); return "Result C"; }) 
).Finally(() => Console.WriteLine("Done!"));

foreach (string r in o.First())
    Console.WriteLine(r);

Result
Executing 1st on Thread: 3
Executing 2nd on Thread: 4
Executing 3rd on Thread: 3
Done!
Result A
Result B
Result C
 
 
摘自:http://rxwiki.wikidot.com/101samples
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值