Rx and MEF are my two favourite new additions to the v4 BCL. Just awesome to see so much care, thought and love going into the platform.
Could you (would you) use RX for file or socket I/O?
If so how would you compare it to IO Completion Ports?
As Rx is a library to orchestrate, not perform asynchronous operations, you would use the Async IO features from .NET and use Rx to react to the completion of the IO:standard async IO:
var fs = new FileStream(@"d:\temp\test.txt", FileMode.Open, FileAccess.Read, FileShare.Read, 1, true); var data = new byte[fs.Length]; fs.BeginRead(data, 0, data.Length, (result) => { var bytesRead = fs.EndRead(result); Console.WriteLine("{0} bytes read.", bytesRead); }, null);
with Rx:
var fs = new FileStream(@"d:\temp\test.txt", FileMode.Open, FileAccess.Read, FileShare.Read, 1, true); var data = new byte[fs.Length]; var asyncRead = Observable.FromAsyncPattern<byte[], int, int, int>( fs.BeginRead, iasyncResult => fs.EndRead(iasyncResult)); var result = asyncRead(data, 0, data.Length); result.Subscribe(bytesRead => Console.WriteLine("{0} bytes read.", bytesRead));
Now this is a small sample, but the main difference is that in the latter, the result of the async operation is a first-class value. This means that you can pass the event stream around, do various operations on it (such as select, merge, selectmany, zip etc...), post the result to the UI thread, etc...
in a bit of shameless self promotion, check out my project
http://geny.codeplex.com/
its a code generator that generate observable versions of various other things such as events
the generation doesnt quite work yet because i needed a release like this to make it work, but check back soon
a question, Rx is available for .net3.5 but it uses pfx, how does that work? is there a version of pfx included in the .net 3.5 release?
--edit--
yes it is
--edit2--
http://geny.codeplex.com/ has now been updated to wor with the new Rx release! there is still no release and im planning to backport it to vs2008 [might be trivial though] but the trunk is now able to create extension methods that return observable methods from events
Awsome. I'm definatly going to check it out =)
Hey folks,
Where can we find the Rx dev labs? I've looked around (e.g. http://msdn.microsoft.com/en-us/aa570323.aspx) but can't find them anywhere!!
-Jamie
http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx
var task1 = Task.Create(_ => { "Thanks for back porting TPL to 3.5!".WriteLine(); }); var task2 = Task.Create(_ => "Thanks for Rx Erik!".WriteLine()); Task.WaitAll(task1, task2); "Thanks to all for these wonderful things!".WriteLine();
BTW - what happened to Task.Factory? I don't see factory class in these Rx 3.5 bits. But the docs are full of factory samples? What am I missing?