Shift my writing to Medium


I shift my posts from this blog to Medium. I will still post a reference to my new post written at Medium. Today I wrote about Span<T>. You can check it at And you can find all other posts at
no comments

Task.Delay Challenge Solution


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


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


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


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)


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)


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