Wednesday, December 21, 2011

How async/await will change our world.

well…using Radical AsyncWorker we can write something like:
public void Do()
{
AsyncWorker.Using( this.Id )
.Configure( cfg =>
{
cfg.Before = e =>
{

this.Status = "Running: " + e.Argument;
};

cfg.After = e =>
{
this.Status = "Completed: " + e.Argument;
};
} )
.Execute( e =>
{
//something long running...
Thread.Sleep( 2000 );
} );
}
Cool, interesting, quite easy to read and a really interesting experiment, for me, in writing a complex and intellisense driven fluent interface…
But…
…in a few months everything will drastically change:
public async void Do()
{
this.Status = "Running: " + this.Id;

await Task.Factory.StartNew( () =>
{
//something long running...
Thread.Sleep( 2000 );
} );

this.Status = "Completed: " + this.Id;
}
Amazing Smile
.m

3 comments:

  1. Imported comment, original author: Marco De Sanctis

    Eh sì, fighissimo! L'unica cosa che mi fa paura è cosa accade se per sbaglio dimentichi quell'await...

    brrrrrr!!

    ReplyDelete
  2. Imported comment, original author: Mauro Servienti

    warning CS1998:
    This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await TaskEx.Run(...)' to do CPU-bound work on a background thread

    ReplyDelete
  3. Imported comment, original author: Gian Maria Ricci

    The introduction of Async/Await is one of the most awesome news of the c#, because it makes async programming so easy.

    I've similar syntax in my project and await is a really improvement.

    ReplyDelete