Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

C# Async Await Pattern Without Task. Making the Crawler-Lib Workflow Elements Awaitable

, 25 Jul 2014 Public Domain
Rate this:
Please Sign up or sign in to vote.
C# 5.0 has introduced the async / await pattern. Where the keyword async specifies that a method, lambda expression or anonymous method is asynchronous. In fact it says that you can use the await keyword to await something. In case of a Task it is the competition.This is how await works: var result

C# 5.0 has introduced the async / await pattern. Where the keyword async specifies that a method, lambda expression or anonymous method is asynchronous. In fact it says that you can use the await keyword to await something. In case of a Task it is the competition.

This is how await works:

  var result = await awaitableObject;

This code is roughly transformed in something like this:

var awaiter = awaitableObject.GetAwaiter();
if( ! awaiter.IsCompleted )
{
  SAVE_STATE()
  (INotifyCompletion awaiter).OnCompleted( continuation)
  return;
}
continuation.Invoke()
... 
continuation = () =>
{
  RESTORE_STATE()
  var result = awaiter.GetResult();
}

In fact everything can be made awaitable, even existing classes without modifying them. An object becomes awaitable when it implements a GetAwaiter() method that returns an awaiter for the class. This can be done with an extension method, so the class itself needs not to be modified. The returned awaiter must implement the following things:

bool IsCompleted { get; }
TResult GetResult(); // TResult can also be void
// and implement ethier the INotifyCompletion or ICriticalNotifyCompletion interface

So, what can we do with it without tasks?

We use it in the Crawler-Lib Framework to make our workflow elements awaitable.

  await new Delay(3000);
  // Work after Delay

instead of providing a handler

  new Delay(3000, delay => { 
    // Work after Delay
  });

That avoids the deeply nested delegates - and lambda expressions - in case of long sequences. The user of the framework can implement sequences very nice, readable and debugable. We will also use it for our upcoming Storage Operation Processor component, to provide a convenient war to write sequences of storage operations, that are internally queued and optimized.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

Share

About the Author

I'm working on a new project called Crawler-Lib. It is a generalized back-end processing and hosting framework for Microsoft .NET and Mono. Please take a look at it:
Crawler-Lib Homepage
Crawler-Lib Blog
Crawler-Lib YouTube Channel
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 25 Jul 2014
Article Copyright 2014 by Thomas Maierhofer (Tom)
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid