Skip to main content

Posts

Showing posts with the label Async

Using GetRequestStreamAsync and GetResponseAsync in .Net 4.0 Portable Class Library

I've been building a small Portable Class Library  and it makes use of the WebRequest class. I'm targeting .Net 4.0 (and above), Silverlight 5 and Windows App Store. I would target the phone as well but it doesn't support the TPL  at the moment - WP8 is just around the corner: I wanted to use async method GetRequestStreamAsync  & GetResponseAsync but these aren't supported in .Net 4.0 and when creating a Portable Class Library you only get the commonly supported methods across the configured platforms - so no support then or may be not... Why not just create a couple of extension methods? 1: public static class HttpWebRequestExtensions 2: { 3: public static Task<Stream> GetRequestStreamAsync( this HttpWebRequest request) 4: { 5: var tcs = new TaskCompletionSource<Stream>(); 6:   7: try 8: { 9: request.BeginGetRequestStream(iar => ...

Testing time based observable in Rx is so easy...

If you've been doing Rx for a while it's very likely you're also be into testing with TDD. In which case you'll have come across testing observable timers but if not then what follows is how easy this is. So lets say I want to test the following method, it generates a 'tick' according to the time span parameter: 1: public IObservable<Unit> TickEvery(TimeSpan timeSpan) 2: { 3: return Observable.Interval(timeSpan).TimeInterval() 4: .Select(_ => new Unit()); 5: } When testing this I don't want to be dependent on the scheduler clock, what I mean is you don't want the test code to have to do some kind of 'wait' operation whilst the Observable.Interval is generating values. The answer is to use the Reactive Extensions Testing Library . It provides the TestScheduler  which allows to manipulate the underlying clock using the AdvanceBy & AdvanceTo methods which then means you can trigger any...

Celebrity Async Deathmatch - round 1

I'm working on an app with lots of asynchronous stream processing done using Rx (Reactive Extensions) - IObservable<T> method. We were discussing the other day whether to replace the implementations which only return a single data item via the Rx stream with the more standard TPL (Task Parallel Library) Task<T> method. We came to the conclusion we wouldn't make the switch for a couple reasons, firstly keeping the code consistency, we're using Rx everywhere for async so why change; and secondly with a possibly more important reason because performance isn't an issue (at the moment). Then I thought.... What's the performance difference between IObservable<T> and Task <T>  for a single async invocation? A simple console app should do, running IObservable<T> vs Task<T> and the quickest wins - reminds me of Celebrity Deathmatch ... Firstly we need something to test, calculating the first 100 primes: All I need now is...