Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basics of using the Microsoft Async Framework

Using Visual Studio Async CTP (Version 3) I am struggling to understand how I can "wrap" existing code using this framework.

For example

Using the OpenPop.NET library I am trying to establish a connection with a pop3 server and confirm I have a valid username and password.

So lets say I have some code like this.

    public bool ConnectSync()
    {
        bool success = true;
        Pop3Client client = new Pop3Client();

        try
        {
            client.Connect("mail.server.com", 110, false);
            client.Authenticate("username", "password");
        }
        catch
        {
            success = false;
        }
        return success;
    }

And now I want to make it Async my understanding from what I have been reading and piecing together is that I would end up with a method signature along the lines of

    public async Task<bool> ConnectAsync()
    {

    }

I believe this is the correct signature because it will be a task that returns a boolean(?) and my guess is that I will need to utilize the TaskEx.Run() method? but that's as far as I can seem to get my head around. Could anyone point in the right direction?

like image 861
Maxim Gershkovich Avatar asked Jan 04 '12 10:01

Maxim Gershkovich


2 Answers

Yes, you're right so far.

The easy way to convert your method is, as you say, just to wrap it in TaskEx.Run so the method runs on a thread pool thread and doesn't block your UI thread.

public Task<bool> ConnectAsync()
{
    return TaskEx.Run( () =>
        {
            bool success = true;
            Pop3Client client = new Pop3Client();

            try
            {
                client.Connect("mail.server.com", 110, false);
                client.Authenticate("username", "password");
            }
            catch
            {
                success = false;
            }
            return success;
        }
    );
}
like image 62
Nick Butler Avatar answered Oct 12 '22 04:10

Nick Butler


Fundamentally, in order to reap the most benefits from the async CTP you could really do with async calls all the way down. You can wrap your synchronous ConnectSync method in a task very easily, without using the async CTP at all:

// Note: not an async method in itself
public Task<bool> ConnectAsync()
{
    return Task.Factory.StartNew<bool>(ConnectSync);
}

(It's possible that you don't have to specify the type argument here - I can never remember the exact rules for type inference using method group conversions.)

That will still tie up a thread while it's connecting though. If you're okay with that, then at least you'll end up with a method you can call from an async method and await appropriately. If nothing else, this will allow you to build the rest of your application upwards using async, and then if/when the POP3 code supports async, you can just rewrite ConnectAsync to be an async method.

like image 27
Jon Skeet Avatar answered Oct 12 '22 05:10

Jon Skeet