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

Rx Challenge #9: Execute Async Until [Solution]

Tuesday, August 30, 2016

Rx Challenge #9: Execute Async Until Challenge #9 was about implementing a pattern where you have to keep sending async message until you will get notification which approve that the message is handled by the other side. see the full detail at the Challenge at this post. Simple scenario For the simple scenario of you calling service and retry sending, according to it’s response. You can use the following pattern. Code Snippet private static async Task VerySimpleScenario() {     var delay = Observable.Timer(TimeSpan.FromSeconds(1));     var noDelay = Observable.Return(1L);     var xs = Observable.FromAsync(() => ExecSimpleAsync(1))...

Rx Challenge #10: DistinctUntilChanged with expiration period

Friday, August 19, 2016

Rx Challenge #10: DistinctUntilChanged with expiration period This challenge is DistinctUntilChanged  cancellation after a period. When having event stream which may produce items in a row and then be idle for a period. You may want to get item after a period even those it identical the the previous one.   On challenge you need to construct it using composition of existing Rx’s operators. It can be formulate into the following pattern: Code Snippet public static IObservable<T> DistinctUntilChanged<T>(     this IObservable<T> source,     TimeSpan distinctPeriod) {     // TODO: your code }   ...
no comments

Rx Challenge #9: Execute Async Until

Tuesday, August 16, 2016

Rx Challenge #9: Execute Async Until This time the challenge is for implementing a pattern where you have to keep sending async message until you will get notification which approve that the message is handled by the other side. The idea is to verify that the message was handled by other side without keeping open connection and waiting for response (which already implement by the TCP protocol). This pattern can be useful on many scenarios, For example, any queue mechanism like Service Bus, Rabbit MQ, etc. Using this pattern you can keep sending the message using predefine interval...

Semaphore Slim and Async

Semaphore Slim and Async On this post I will focus on async waiting on Semaphore (using SemaphoreSlim). In general .NET is having 2 Semaphore implementations: - Semaphore which is the legacy implementation (but support cross process operations) - SemaphoreSlim which is lightweight version. It bring some performance improvements but it don’t support cross process operation. this post will focus on SemaphoreSlim WaitAsync API. Let start analyzing the following code snippet, think what’s wrong with it: Code Snippet private static SemaphoreSlim _gate = new SemaphoreSlim(4); static void Main(string args) {     for (int i = 0;...
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...