Reactive Extensions の非同期 については今まで簡単に書こうというつもりがなく動作検証でいっぱいいっぱいだったため短く1行出かけるパターンをお見せしておきたいと思う。
下のコードはDebug.WriteLineを追加してあるので行数は増えているが抜けば1行でも書ける。
Func<int, int> nullFunc = z => {
System.Diagnostics.Debug.WriteLine("FromAsyncPattern start"); Thread.Sleep(1000);
System.Diagnostics.Debug.WriteLine("FromAsyncPattern end"); return z + 1; };
var fromAsyncPattern = Observable.FromAsyncPattern<int,int>(nullFunc.BeginInvoke, nullFunc.EndInvoke);
var fromAsyncDispose = fromAsyncPattern(1).Subscribe(z =>
System.Diagnostics.Debug.WriteLine("FromAsyncPattern result {0}", z)
);
System.Diagnostics.Debug.WriteLine("FromAsyncPattern");
FromAsyncPattern start
FromAsyncPattern
FromAsyncPattern end
FromAsyncPattern result 2