Created
July 30, 2009 10:23
-
-
Save jbevain/158644 to your computer and use it in GitHub Desktop.
A test program to use the RX framework on the .net framework
This file contains 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.Collections.Generic; | |
using System.Linq; | |
// from http://themechanicalbride.blogspot.com/2009/07/introducing-rx-linq-to-events.html | |
class Program { | |
static void Main () | |
{ | |
new NumbersObservable (new [] {2,3,4}).Subscribe (new DebugObserver ()); | |
} | |
} | |
internal class AnonymousDisposable : IDisposable | |
{ | |
internal Action Action {get; set;} | |
void IDisposable.Dispose() | |
{ | |
this.Action(); | |
} | |
} | |
class NumbersObservable : IObservable<int> | |
{ | |
public NumbersObservable(IEnumerable<int> numbers) | |
{ | |
this._numbers = numbers; | |
} | |
private IEnumerable<int> _numbers; | |
public IDisposable Subscribe(IObserver<int> observer) | |
{ | |
foreach(int number in _numbers) | |
{ | |
observer.OnNext(number); | |
} | |
observer.OnCompleted(); | |
return new AnonymousDisposable { Action = () => { ; } }; | |
} | |
} | |
class DebugObserver : IObserver<int> | |
{ | |
public void OnNext(int value) | |
{ | |
Console.WriteLine("{0}", value); | |
} | |
public void OnCompleted() | |
{ | |
Console.WriteLine("all done."); | |
} | |
public void OnError(Exception ex) | |
{ | |
Console.WriteLine("Whoops exception, I'd better throw."); | |
throw ex; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment