Click here to Skip to main content
13,142,182 members (52,217 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


13 bookmarked
Posted 12 Aug 2014

From AsyncCompleted to async await

, 12 Aug 2014
Rate this:
Please Sign up or sign in to vote.
From AsyncCompleted to async await

Since we got the magic of async/await on C#, things got much easier on the developer front. Working with Tasks is the key to it, but we also have two older approaches in .NET framework that we need to deal with.

  1. APM (Asynchronous Programming Model) The pair of Begin/End methods.
  2. EAP (Event-based Asynchronous Programming) The MethodAsync/MethodCompleted pair.

Converting from APM to Tasks is achieved by means of the System.Threading.Tasks.TaskFactory class and its generic counterpart.

Now converting from EAP to Tasks requires us to use the TaskCompletionSource<T> class and notify of the state of the task as it goes. See the example of a "handshake" operation in a WCF service. I am also using the approach from my older post regarding events and anonymous methods. The return type of the WCF operation is WrappedResultOfString, and that will be the actual generic parameter of the Task<T> that the HandshakeAsync method will produce.

 public Task<WrappedResultofString> HandshakeAsync(Credentials credentials)
            var tcs = new TaskCompletionSource<WrappedResultofString>();
            var client = _serviceFactory.Invoke(); //get a WCF client instance
            EventHandler<HandshakeCompletedEventArgs> callback = null;
            callback = (s, e) =>
                client.HandshakeCompleted -= callback;
                 if (e.Error != null)
                    tcs.TrySetResult(new WrappedResultOfstring
                        Value = null,
                        IsFailed = true,
                        Error =  e.Error
                 else if (e.Cancelled)
                    tcs.TrySetResult(new WrappedResultOfstring
                        Value = null,
                        IsFailed = true,
                        Error = "Operation Cancelled"

            client.HandshakeCompleted += callback;
            client.HandshakeAsync(credentials.UserName, credentials.Password);
            return tcs.Task;

//then this is ready to be used with async/await

public async void LoginMethod()
   var credentials = //build your credentials;
  //now we can use 'await'
   var handshake = await this.HandshakeAsync(credentials);

This is particularly useful when dealing with WCF proxies in Silverlight or Windows Phone apps where we only get EAP-based operations.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Leonardo Paneque
United States United States
Leonardo loves to code with C# on any platform and OS.
He has a Master degree in Computer Sciences and likes to share code and ideas.

You may also be interested in...

Comments and Discussions

Questionquestion Pin
VolcOne29-Dec-14 22:52
memberVolcOne29-Dec-14 22:52 
QuestionOne request Pin
Tridip Bhattacharjee12-Aug-14 21:16
memberTridip Bhattacharjee12-Aug-14 21:16 
QuestionOne request Pin
Tridip Bhattacharjee12-Aug-14 21:15
memberTridip Bhattacharjee12-Aug-14 21:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 12 Aug 2014
Article Copyright 2014 by Leonardo Paneque
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid