Task.Delay Challenge

Friday, April 14, 2017

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

Async Tip: why you should avoid void

Wednesday, March 15, 2017

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

Demystify Async and Await (Part 1 of 2)

Tuesday, March 29, 2016

Demystify Async and Await (Part 1 of 2) After seeing so many confusion speed abroad the industry, I had came to conclusion to write this post and demystify the behavior of async and await.   To many people is having to many wrong conceptions and theories about the functionality of async and await. It lead to great confusion and bad practices. In order to explain async and await I must go back to the basic and demystify Task.   Part 1 of this 2 part series will explain what really is Task and the confusion almost everybody...
no comments

Awaiting RX

Saturday, November 21, 2015

Awaiting RX this post discuss the meaning of awaiting RX stream. many languages including .NET embrace the concept of async / await. async / await on .NET is usually refer to Task, but Task in not the only await-able type in .net, in matter of fact there is a few type which can be awaited, one of those type is IObservable.   what happens when you’re awaiting IObservable? When awaiting IObservable the await schedule on completion and resume with the last value. for example: Code Snippet int i = await Observable.Range(0,...
one comment

Service Fabric Concept, Guidelines and Patterns #1

Friday, November 13, 2015

Service Fabric Concept, Guidelines and Patterns #1 this is the first post in series which focus on Service Fabric. the series covers Service Fabric’s concept, guidelines and common patterns.   What is Service Fabric? Service Fabric is new framework that can operate either on the cloud (Azure) or on premise. this framework enable to build solution from small parts (concept known as Micro-Services). Micro-Services can be thought as Object-Oriented for cloud (or distributed application). the most granular unit of Micro-Services called Actor. Actor is similar to class in the Object Oriented paradigm. group of Actor can be packed...

The 3rd day of the SDP

Wednesday, May 8, 2013

The 3rd day of the SDP was over and I want to thanks all the attendants. my yesterday session was about C# 5 async and await. today I will have a full day tutorial on Rx and TPL Dataflow. you can download the demo code and also the presentation for second and third days from here (the link is also having the demo code for today's sessions). anyway, I also want to recommend 2of yesterday session. if you were attend at the conference you should have an access to...

Async / Await for .NET 4, Silverlight and Windows Phone

Tuesday, May 7, 2013

Async / Await for .NET 4, Silverlight and Windows Phone if you have to target .NET 4, Silverlight and Windows Phone and still want to use the async / await pattern. the BCL team provide you with a new NuGet package named Microsoft.Bcl.Async. this package was announced as stable a few week ago. so you can check it out if you're having VS 2012 but should target one of the above platforms. be aware that this package won't work with 2010.
no comments

EF 6: Async

Tuesday, March 19, 2013

EF 6: Async this post is the first in a series about what's new in EF 6. great improvements are about to come with Entity Framework 6. it is a major release and the first one since EF become an open source. each post in the series will be dedicate to a single feature. this post will focus on a new EF a-sync features. the first question that should be asked is, why do we need parallel data access? moreover why...