The System.Net.Sockets.Socket class in .NET exposes multiple sets of asynchronous methods that perform the same basic operations but that are exposed with different patterns.The first set follows the APM pattern, where for a synchronous method like Receive, the BeginReceive and EndReceive methods are exposed. If you want to be able to &...
TPL Dataflow includes a number of built-in, already-implemented blocks that target the most common scenarios. Additionally, some flexibility is provided by the set of options that may be used to tweak block behaviors. However, a developer may still choose to implement a custom block for advanced scenarios where the built-in ones ...
In the .NET Framework 4.5 Developer Preview, you’ll find that CancellationTokenSource now has timeout support built directly into its implementation. This makes it very easy to create a token that will automatically have cancellation requested after a particular time interval, e.g.
public static CancellationToken FromTimeout(int ...
Available since .NET 4, ThreadLocal<T> is a container that holds a separate value for every thread. In practice, ThreadLocal<T> is often convenient for storing per-thread counters, resources, or partial results. As mentioned earlier on this blog, we have been thinking about adding a Values property to enumerate over the values ...
One interesting thing to know about PLINQ is that not all queries are guaranteed to execute in parallel (See PLINQ Queries That Run Sequentially for reference). You can think of the AsParallel method as a hint to run in parallel for query shapes that it believes will be faster. By default, PLINQ prefers to use a simple sequential algorithm ...
We invested a lot of time into making parallel programming “just faster” for .NET 4.5. You’ve already seen some neat tricks to ConcurrentDictionary. There’s a lot more to say about improving the performance of the Task Parallel Library, and Joe Hoag has a written an excellent paper on the subject:TPL ...
Imagine that you have a Task handed to you by a third party, and that you would like to force this Task to complete within a specified time period. However, you cannot alter the “natural” completion path and completion state of the Task, as that may cause problems with other consumers of the Task. So you need a way to obtain a copy or “...
A “dataflow block” is represented by a class implementing the IDataflowBlock interface. The state of a dataflow block is represented by the state of its IDataflowBlock.Completion Task, which itself has a Status property. When a dataflow block is in active state, meaning that it is currently doing processing or may do more processing in the...
ConcurrentDictionary is a popular concurrent data structure that was introduced in .NET 4. In the .NET 4.5 release, ConcurrentDictionary gets two performance improvements. One optimization is related to the way ConcurrentDictionary avoids torn reads and writes. To explain the background, all reference types and some value types are guaranteed...
When you start using async methods heavily, you’ll likely see a particular pattern of composition pop up from time to time. Its structure is typically either of the form: async Task FooAsync() { … // some initialization code without awaits await BarAsync(&hellip...