.NET Blog

Free. Cross-platform. Open source. A developer platform for building all your apps.

Featured posts

Announcing .NET 11
Nov 10, 2026
Post comments count 4
Post likes count 25

Announcing .NET 11

.NET Team

.NET 11 is here with C# 15, faster developer tools, runtime and library improvements, and new capabilities for web, mobile, desktop, and AI apps.

.NETASP.NET CoreC#

Latest posts

Jan 20, 2012
Post comments count 0
Post likes count 0

Implementing a SynchronizationContext.SendAsync method

Stephen Toub - MSFT

I recently saw two unrelated questions, the answers to which combine to form a potentially useful code snippet.The first question was about SynchronizationContext.  SynchronizationContext provides a Post method, which asynchronously schedules the supplied delegate and object state to be executed according to the SynchronizationContext’s whims.  However, Post returns void, so there’s no built-in way to know when the operation has completed.  SynchronizationContext also provides a Send method, which won’t return until the delegate has been executed; that means that while you do have...

Jan 20, 2012
Post comments count 4
Post likes count 4

Await, SynchronizationContext, and Console Apps

Stephen Toub - MSFT

When I discuss the new async language features of C# and Visual Basic, one of the attributes I ascribe to the await keyword is that it “tries to bring you back to where you were.” For example, if you use await on the UI thread of your WPF application, the code that comes after the await completes should run back on that same UI thread. There are several mechanisms that are used by the async/await infrastructure under the covers to make this marshaling work: SynchronizationContext and TaskScheduler. While the transformation is much more complicated than what I’m about to show, logically you can t...

Jan 14, 2012
Post comments count 0
Post likes count 0

FAQ on Task.Start

Stephen Toub - MSFT

Recently I’ve heard a number of folks asking about Task.Start, when and when not to use it, how it behaves,and so forth.  I thought I’d answer some of those questions here in an attempt to clarify and put to rest any misconceptions about what it is and what it does.1. Question: When can I use Task.Start?The Start instance method may be used if and only if the Task is in the Created state (i.e. Task.Status returns TaskStatus.Created).  And the only way a Task can be in the Created state is if the Task were instantiated using one of Task’s public constructors, e.g. "var t = new Task(some...

Dec 15, 2011
Post comments count 0
Post likes count 0

Awaiting Socket Operations

Stephen Toub - MSFT

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 “await” such asynchronous operations in an async method, the easiest way to do so is to create a Task-based wrapper.  That can be done using Task.Factory.FromAsync, or it can be done more manually by using a TaskCompletionSource<TResult>, e.g. public sta...

Dec 5, 2011
Post comments count 0
Post likes count 0

Paper :: Guide to Implementing Custom TPL Dataflow Blocks

Danny Shih

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 are not sufficient.  Zlatko Michailov explores the options for achieving this in his paper:Guide to Implementing Custom TPL Dataflow BlocksEnjoy!

Dec 3, 2011
Post comments count 0
Post likes count 0

Coalescing CancellationTokens from Timeouts

Stephen Toub - MSFT

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 millisecondsDue) {     return new CancellationTokenSource(millisecondsDue).Token; } Under the covers here, CancellationTokenSource is creating and configuring a new System.Threading.Timer to call the source’s Cancel method after the particular duration pas...

Nov 11, 2011
Post comments count 0
Post likes count 0

How to use C++ AMP from C# using WinRT

Igor Ostrovsky - MSFT

[Updated 5/17/2012 for Visual Studio 11 Beta] In a previous article, How to use C++ AMP to C#, we described how you can use P/Invoke to call into C++ AMP and accelerate your C# apps on GPUs and other heterogeneous hardware. In this post, we'll take a look at how the same task becomes easier in Windows 8 using WinRT. Before attempting to call C++ AMP from C#, make sure that you have C++ AMP working on your machine. For example, please verify that you can run the C++ AMP "Hello, World" example. The short story Once you have C++ AMP working on your machine, the easiest way to start using it from C# via WinRT is to o...

Nov 10, 2011
Post comments count 0
Post likes count 0

New in .NET 4.5: ThreadLocal.Values

Igor Ostrovsky - MSFT

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 from all threads that ever stored a value into the ThreadLocal<T> instance. After getting helpful feedback from this blog and other sources, we ended up implementing the capability in .NET 4.5. To use the Values property, you have to initialize the ThreadLocal<T> i...

Nov 10, 2011
Post comments count 0
Post likes count 0

PLINQ Queries That Run in Parallel in .NET 4.5

Igor Ostrovsky - MSFT

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 over a potentially expensive parallel algorithm, and so some queries will execute sequentially, especially queries where the chain of operators do not lend themselves to parallelism. The bottom line is that PLINQ’s intent is to run your queries as fast as possible, so if there’...