使用 Microsoft 异步框架的基础知识

发布于 2024-12-24 21:36:57 字数 759 浏览 1 评论 0原文

使用 Visual Studio Async CTP(版本 3),我很难理解如何使用此框架“包装”现有代码。

例如,

使用 OpenPop.NET 库,我尝试与 pop3 服务器建立连接,并确认我拥有有效的用户名和密码。

假设我有一些这样的代码。

    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;
    }

现在我想让它异步,根据我一直在阅读和拼凑的内容,我的理解是我最终会得到一个方法签名,

    public async Task<bool> ConnectAsync()
    {

    }

我相信这是正确的签名,因为它将是一个返回布尔值的任务(?)我的猜测是我需要使用 TaskEx.Run() 方法?但这是我所能想到的。有人能指出正确的方向吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

惯饮孤独 2024-12-31 21:36:57

是的,到目前为止你是对的。

正如您所说,转换方法的简单方法是将其包装在 TaskEx.Run 中,以便该方法在线程池线程上运行,并且不会阻塞您的 UI 线程。

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;
        }
    );
}

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;
        }
    );
}
忘你却要生生世世 2024-12-31 21:36:57

从根本上讲,为了从异步 CTP 中获得最大收益,您实际上可以一直使用异步调用。您可以非常轻松地将同步 ConnectSync 方法包装在任务中,而无需使用异步 CTP:(

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

您可能不必在此处指定类型参数- 我永远记不起使用方法组转换进行类型推断的确切规则。)

但这仍然会在连接时占用线程。如果您对此表示同意,那么至少您最终会得到一个可以从异步方法调用并适当等待的方法。如果不出意外,这将允许您使用异步向上构建应用程序的其余部分,然后如果/当 POP3 代码支持异步时,您可以将 ConnectAsync 重写为异步方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文