background preloader

Rx

Facebook Twitter

C#/.NET Little Wonders: ConcurrentBag and BlockingCollection. In the first week of concurrent collections, began with a general introduction and discussed the ConcurrentStack<T> and ConcurrentQueue<T>. The last post discussed the ConcurrentDictionary<T> . Finally this week, we shall close with a discussion of the ConcurrentBag<T> and BlockingCollection<T>. For more of the "Little Wonders" posts, see C#/.NET Little Wonders: A Redux. Recap As you'll recall from the previous posts, the original collections were object-based containers that accomplished synchronization through a Synchronized member. With .NET 4.0, a new breed of collections was born in the System.Collections.Concurrent namespace.

For some excellent information on the performance of the concurrent collections and how they perform compared to a traditional brute-force locking strategy, see this informative whitepaper by the Microsoft Parallel Computing Platform team here. ConcurrentBag<T> – Thread-safe unordered collection. So why would you ever want a container that can be unfair? 16: else. Scheduling - Design for task scheduler in .NET 4 (Task Parallel Library)

.NET 4 Cancellation Framework - Parallel Programming with .NET. A very interesting addition to .NET 4 is a set of new types that specifically assist with building cancellation-aware applications and libraries. The new types enable rich scenarios for convenient and safe cancellation, and help simplify situations that used to be be difficult and error-prone and non-composable. The details of the new types are described below, but lets begin with some of the motivating principles that the new types were designed to support: When any unit of work is commenced, it should have a consistent means to support early termination in response to a cancellation request. If some unit of work controls various other moving pieces, it should be able to conveniently chain cancellation requests to them.

Blocking calls should be able to support cancellation. Calls to complex operations, such as MoveNext() on a PLINQ enumerator, should have simple yet comprehensive cancellation support. Previous approaches New cancellation types Details Advanced patterns Conclusion. Driven mad by subscribeOn, ObserveOn. Is it possible you have subscribed > 1? James Miles  I am pretty sure not. There is only one piece of code in the viewmodel with subscribe in it, and there is a single log call there to confirm it is only called once. Difficult to post the whole set of code, extensions and all, but this is the base class from which each element is derived: public class CompoundSeriesBase<S, T> : IDisposable, ICompoundProducer<T> { internal IEnumerable<S> _source; internal ICompoundProducer<S> _producer; private bool _disposed; public virtual IEnumerable<T> GetData(CancellationToken token) { Log.Warning(null,null,null,"GetData() called"); _source = _producer == null ?

Foreach (S item in _source) { yield return (Process(item)); } } protected virtual T Process(S item) { Log.Warning(null, null, null, "Process Called, Not Implemented"); throw new NotImplementedException(); } (originator) and, in the inline element: How to wait for an IObservable to complete before returning data in a WCF service? System.reactive - rx reactive extension: how to have each subscriber get a different value (the next one) from an observable. The Joy of Rx: The Event-based Async Pattern vs. IObservable. In part 1 we talked about how to convert events to IObservables. However Rx isn't just about querying events, it's also about querying asynchronous operations. There are two common asynchronous patterns in the .NET framework: the event-based asynchronous pattern and Begin/EndInvoke. Currently MSDN recommends using the event-based asynchronous pattern where possible. This recommendation may or may not change when IObservable is released with .NET 4.0.

I want to make clear that the opinions expressed in this post are my own personal views and are not consistent with official MSDN guidance. Don't use the event-based asynchronous pattern. A thoroughly awful piece of code no? Of course it's rather cumbersome to create a new class that inherits from IObservable whenever we need to return the result of an asynchronous operation. Now that we've got our handy, reusable AnonymousObservable class we're ready to create our GetDownloadString extension event. Pretty slick right?