AsyncCTP:创建一类就是IAwaitable [英] AsyncCTP: Creating a class that is IAwaitable

查看:191
本文介绍了AsyncCTP:创建一类就是IAwaitable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己想实现一个IAwaitable类(一些impliments异步调用不会阻塞线程)。

我已经安装了该AsyncCTP的最新版本,编译器是说,我需要一个IsCompleted()成员。好了,所以CTP preVIEW有一点点移动(我得到的,就像是一个preVIEW)

问:什么界面都AsyncCTP语言扩展期待的现在

问:在这一切的我假设我可以通过LAMDA /委派信号的IAwaitable?这可能吗?难道我们称之为EndAwait?智能感知建议您拨打EndAwait检索结果......所以不健全的权利。任何想法?

所有的我发现迄今是为特征的AsyncCTP库已经形式实现,如例子:

 等待新的WebClient()DownloadStringTaskAsync(URI).ConfigureAwait(假)。

101 AsyncSamplesCS

背景:

我发现自己乔恩长柄水杓页面(再次)上寻找<一href=\"http://msmvps.com/blogs/jon_skeet/archive/2010/10/31/c-5-async-experimenting-with-member-resolution-getawaiter-beginawait-endawait.aspx\"相对=nofollow>这个例子

 使用系统;类测试
{
    异步静态无效的主要()
    {
        等待新Awaitable();
    }
}类Awaitable
{
    公共Awaiter GetAwaiter()
    {
        返回新Awaiter();
    }
}类Awaiter
{
    公共BOOL BeginAwait(动作续)
    {
        返回false;
    }    公众诠释EndAwait()
    {
        返回1;
    }
}


解决方案

随着SP1的更新,您需要:


  • 返回的东西有些 GetAwaiter()方法(可能但不一定扩展方法)( Awaiter 在你的例如)与所有的:

    • A 布尔IsCompleted 属性( GET

    • A 无效OnCompleted(动作回调)

    • A 调用getResult()方法,它返回无效,或等待操作所期望的结果


不过,我建议你看看 TaskCompletionSource&LT; T&GT ; - 我看了在这一点,它的我的幼稚实施出执行(这里;过时)。你也可以将它用于无效任务,通过使用类似一个 TaskCompletionSource&LT;布尔&GT; (和利用的事实,在任务&LT;布尔方式&gt; 也是一个非类型化工作

I found myself wanting to implement an IAwaitable class (something that impliments asynchronous calls without blocking threads).

I've got the most recent version of AsyncCTP installed, and the compiler is saying that I need an IsCompleted() member. Okay, so the CTP preview has moved on a little bit (I get that, like it's a preview)

Question: What interface are the AsyncCTP language extensions expecting now?

Question: In all this I'm assuming that I can signal to the "IAwaitable" via a lamda/delegate? Is this possible? Do we call EndAwait? The intellisense suggests that you call EndAwait to retrieve the result... so that doesn't sound right. Any ideas?

All of the examples I've found so far are for features that the AsyncCTP library has already implimented such as:

  await new WebClient().DownloadStringTaskAsync(uri).ConfigureAwait(false);

from the 101 AsyncSamplesCS

Background:

I find myself on Jon Skeets page (again) looking at this example

using System;

class Test
{
    static async void Main()
    {
        await new Awaitable();
    }
}

class Awaitable
{
    public Awaiter GetAwaiter()
    {
        return new Awaiter();
    }
}

class Awaiter
{
    public bool BeginAwait(Action continuation)
    {
        return false;
    }

    public int EndAwait()
    {
        return 1;
    }
}

解决方案

With the SP1 refresh, you need:

  • Some GetAwaiter() method (possibly but not necessarily an extension method) that returns something (Awaiter in your example) with all of:
    • A bool IsCompleted property (get)
    • A void OnCompleted(Action callback)
    • A GetResult() method which returns void, or the desired outcome of the awaited operation

However, I suggest you look at TaskCompletionSource<T> - I looked at this, and it out-performed my naive implementation (here; obsolete). You can also use it for void tasks, by using something like a TaskCompletionSource<bool> (and exploit the fact that the Task<bool> is also an untyped Task).

这篇关于AsyncCTP:创建一类就是IAwaitable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆