Task.Delay Challenge Solution

2017/04/21

Task.Delay Challenge Solution This post suggest solution to the Task.Delay challenge. If you haven’t read the Task.Delay challenge you can find it here. In nutshell the challenge was about making Task.Delay like functionality which is test-able (using IScheduler for having virtual time). In order to support virtual time I will use Rx’s stream (which is also awaitable). The original Task.Delay is actually having 2 different completion path: - Complete by time. - Complete as result of cancellation token event. The general idea of the solution is to have 2 Rx’s streams one for each completion path: - Timer...
no comments

Task.Delay Challenge

2017/04/14

Task.Delay Challenge Task.Delay can be useful in many scenarios, for example: periodic execution.  Traditionally periodic execution done by using Timer API. Periodic execution with Task.Delay is easy for debug and better for maintain. The following code show the pattern: Code Snippet private async Task PeriodicJobAsync(CancellationToken token) {     var duration = TimeSpan.FromSeconds(5);     while (!token.IsCancellationRequested)     {         try         {             await Task.Delay(duration, token);         }         catch (OperationCanceledException) { }           // Process the job     } } The problem start when you want to test the code without waiting the real-world duration.   The challenge...
no comments

SDP Teaser

2017/04/04

This is a short teaser for what I’m planning to talk about in the next upcoming SDP conference. If you happened to be in Israel on May 2017 you can still register to the talk. The audience for this session expected to have prior knowledge about TPL,  async / await and parallel programming in general. On this full day workshop I will focus on the less familiar aspects and patterns.   Beyond Async And Await Planed Agenda * Task from different angle * Task.Run vs Task.Factory.StartNew * async / await drill down and custom awaitable * Some...

Async Tip: why you should avoid void

2017/03/15

Async Tip: why you should avoid void The guideline to prefer async method which return Task over async method that return void is quite known, but why should you follow the guideline? Code Snippet public async void NotRecomendedAsync() {     // do something     await SomeCodeWhichMayThrowAsync();     // do something }   public async Task RecomendedAsync() {     // do something     await SomeCodeWhichMayThrowAsync();     // do something } The first obvious reason to use the guideline is testability. When you return void the test don’t really know when it is right time to check...
no comments

Task.Run vs. Task.Factory.StartNew (Part 2)

2017/01/26

Task.Run vs. Task.Factory.StartNew (Part 2) This post continue the previous post about the differences between Task.Run vs. Task.Factory,StartNew. In the previous part I was speaking about the scheduling aspect, on this part I will spot the async await behavior.   Check the following code snippet and think what can go wrong: Code Snippet private async Task ExecuteAsync() {     var sw = Stopwatch.StartNew();     await Task.Factory.StartNew(async () =>         {             Console.WriteLine("Start section");             await Task.Delay(1000);             Console.WriteLine("End section");         });     Console.WriteLine(sw.Elapsed); }   What output will it produce? Will it be something like the following output: ...
one comment

Task.Run vs. Task.Factory.StartNew (Part 1)

2017/01/24

Task.Run vs. Task.Factory.StartNew (Part 1) This post will focus on the key difference between Task.Run and Task.Factory.StartNew. For you can drill down into more details on this excellent post by Stephen Toub. In general you have to know that Task.Run is more restricted version of Task.Factory.StartNew. Task.Run sets default which fit for most common cases. Task.Factory.StartNew is there for advance scenarios.   in general Task.Run is equivalent to: Code Snippet Task.Factory.StartNew(someAction,                 cancellationToken,                 TaskCreationOptions.DenyChildAttach,                 TaskScheduler.Default);   Why should you care about it and how can it be used right?...
no comments

NuGet Extensions

2016/11/17

Click Dimensions open source the first preview of NuGet Extensions tool which is used internally for managing projects which deployed as NuGet. The development process of solution which is using NuGet of internal component is not easy when Ii t come to debug and upgrade. Consider the case when you’re debugging the application and you find out that there is a bug on the infrastructure. In case when the infrastructure is consume via NuGet you will have to: - Fix the bug. - Pack new version of the infrastructure (NuGet). - Update the application. - Re-test it with the...
no comments

Rx Challenge #11–Solution

2016/09/15

Rx Challenge #11–Solution You can find the solution for Challenge 11 below. Previous challenges available here. The following code snippet implement the base functionality which is similar to the functionality of SemaphoreSlim. Code Snippet public class AsyncLock {     private int _counter = 0;     private readonly int _limit;     // semantic Task     private ConcurrentQueue<TaskCompletionSource<object>> _tcsQueue =         new ConcurrentQueue<TaskCompletionSource<object>>();       #region Ctor       /// <summary>     /// Initializes a new instance of the <see cref="AsyncLock"/> class.     /// </summary>     /// <param name="limit">The limit.</param>     public AsyncLock(int limit)     {         _limit = limit;     }       #endregion // Ctor       #region WaitAsync  ...
no comments