- Dev Blogs
- .NET Blog
No trial. No credit card required. Just your GitHub account.
.NET Blog
Free. Cross-platform. Open source. A developer platform for building all your apps.
Latest posts

How to create a Web Deploy package when publishing a ClickOnce project
The other day I saw a question on StackOverflow (link in resources below) asking How you can create a Web Deploy (AKA MSDeploy) package when publishing a ClickOnce project. The easiest way to do this is to use the Web Deploy command line utility, msdeploy.exe. With the command line you can easily create an MSDeploy package from a folder with a command like the following: Here you can see that I’m using the sync verb, along with a contentPath provider (which points to a folder) as the source and the destination is using the package provider, this point to where I want the package to be stored. Now that...

Building Async Coordination Primitives, Part 7: AsyncReaderWriterLock

In my last past, we looked at building an AsyncLock in terms of an AsyncSemaphore. In this post, we’ll build a more advanced construct, an asynchronous reader/writer lock.An asynchronous reader/writer lock is more complicated than any of the previous coordination primitives we’ve created. It also involves more policy, meaning there are more decisions to be made about how exactly the type should behave. For the purposes of this example, I’ve made a few decisions. First, writers take precedence over readers. This means that regardless of the order in which read or wri...

Building Async Coordination Primitives, Part 6: AsyncLock

Last time, we looked at building an AsyncSemaphore. Here, we’ll look at building support for an async mutual exclusion mechanism that supports scoping via ‘using’. As mentioned in the previous post, semaphores are great for throttling and resource management. You can give a semaphore an initial count of the number of things to protect, and then it’ll only allow that many consumers to successfully acquire the semaphore, forcing all others to wait until a resource is freed up and count on the semaphore is released. That resource to protect could be the right to enter a particular region of code, and the count c...

Building Async Coordination Primitives, Part 5: AsyncSemaphore

In my last few posts, I covered building an AsyncManualResetEvent, an AsyncAutoResetEvent, an AsyncCountdownEvent, and an AsyncBarrier. In this post, I’ll cover building an AsyncSemaphore class.Semaphores have a wide range of applicability. They’re great for throttling, for protected access to a limited set of resources, and more. There are two public semaphore types in .NET: Semaphore (which wraps the Win32 equivalent) and SemaphoreSlim (which provides a lightweight counterpart built around Monitors). Here we’ll build a simple async version, with the following shape: p...

Building Async Coordination Primitives, Part 4: AsyncBarrier

Last time, we looked at building an AsyncCountdownEvent. At the end of the post, I highlighted a common pattern for using such a type, which is for all of the participants to signal and then wait for all of the other participants to signal as well. This kind of synchronization is typically referred to as a “barrier,” and often it can be used in a round. Each participant will do some work, then signal-and-wait for the barrier, then do the next round of work, then signal-and-wait for the barrier, and so on.Just as we built an AsyncCountdownEvent, we can easily build an AsyncBarrier.&nb...

Building Async Coordination Primitives, Part 3: AsyncCountdownEvent

In my last two posts, I discussed building AsyncManualResetEvent and AsyncAutoResetEvent coordination primitives. In this post, I’ll build on that to create a simple AsyncCountdownEvent.A countdown event is an event that will allow waiters to complete after receiving a particular number of signals. The “countdown” comes from the common fork/join pattern in which it’s often utilized: a certain number of operations participate, and as they complete they signal the event, which counts down from the original number to 0. When it gets to 0, it becomes set, and all waiters can ...

Building Async Coordination Primitives, Part 2: AsyncAutoResetEvent

In my last post, I discussed building an asynchronous version of a manual-reset event. This time, we’ll build an asynchronous version of an auto-reset event.A manual-reset event is transitioned to the signaled state when requested to do so (i.e. calling Set()), and then it remains in that state until it’s manually transitioned back to the non-signaled state (i.e. a call to Reset()). In contrast, an auto-reset event also transitions to the signaled state when requested to do so, but it then transitions back to the non-signaled state automatically when a wait operation completes due to that ...

Building Async Coordination Primitives, Part 1: AsyncManualResetEvent

The Task-based Async Pattern (TAP) isn’t just about asynchronous operations that you initiate and then asynchronously wait for to complete. More generally, tasks can be used to represent all sorts of happenings, enabling you to await for any matter of condition to occur. We can even use Tasks to build simple coordination primitives like their synchronous counterparts but that allow the waiting to be done asynchronously.One of the more basic coordination primitives is an event, and there are a few of these in the .NET Framework. ManualResetEvent and AutoResetEvent wrap their Win32 counterparts, a...

Potential pitfalls to avoid when passing around async lambdas

One of the really useful capabilities of the new async methods feature in C# and Visual Basic is the ability to write async lambdas and anonymous methods (from here on in this post, I’ll refer to both of these as async lambdas, since the discussion applies equally to both). This allows you to easily get a delegate to represent an asynchronous operation, e.g. Func<Uri,Task<string>> getContentsLowerCaseAsync = async url => { string contents = await DownloadString(url); return contents.ToLower(); }; Async methods in C# and...