我可以等待的东西,如果没有GetAwaiter方法​​?如果没有、东西、方法、GetAwaiter

2023-09-04 00:12:22 作者:Prostitute▍入戏

我看到了有关设计自定义awaitable类型的一些文章:

I saw some articles about designing a custom awaitable type:

http://books.google.com.br/books?id=1On1glEbTfIC&pg=PA83&lpg=PA83&dq=create+a+custom+awaitable+type

现在考虑下面的例子:

<Button x:Name="BtnA"
        Width="75"
        Margin="171,128,0,0"
        HorizontalAlignment="Left"
        VerticalAlignment="Top"
        Click="BtnA_Click"
        Content="Button A" />
<Button x:Name="BtnB"
        Width="75"
        Margin="273,128,0,0"
        HorizontalAlignment="Left"
        VerticalAlignment="Top"
        Content="Button B"  Click="BtnB_OnClick" />

private async void BtnA_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Awaiting Button B..");

    var sx = Observable.FromEvent<MouseButtonEventHandler, 
                                  MouseButtonEventArgs>(a => (b, c) => a(c),
                                  add => BtnB.PreviewMouseDown += add,
                                  rem => BtnB.PreviewMouseDown -= rem)
       .Do(a => a.Handled = true)
       .Take(1);

    await sx;

    MessageBox.Show("Button B Pressed after Button A");
}

private void BtnB_OnClick(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Button B Pressed Without Click in Button A");
}

为什么可以等待的IObservable&LT; T&GT; (在当认购完成后这种情况下),如果没有 GetAwaiter()的方法?

Why can I await IObservable<T> (in this case when subscription is completed), if there's no GetAwaiter() method?

这是由编译器实现?是否有可能实现,能伺机,但无明确的这种方法(正在使用某些情况下它的接口)的东西吗? 为什么是不是有这样的:

Is it implemented by the compiler? Is it possible to implement something that can await without to explicit this method (some scenarios which interfaces is being used)? And why isn't there something like:

public interface ITask<out T>
{
    IAwaiter<T> GetAwaiter();
}

public interface IAwaiter<out T> : ICriticalNotifyCompletion
{
    bool IsCompleted { get; }
    T GetResult();
}

...或真正的接口来创建自定义awaitable?

... or real interfaces to create a custom awaitable?

推荐答案

您不能计谋的的东西的不具有 GetAwaiter 方法该收益的的东西的,有调用getResult OnCompleted IsCompleted 和农具 INotifyCompletion

First of all, no.

You can't await something that doesn't have a GetAwaiter method that returns something that has GetResult, OnCompleted, IsCompleted and implements INotifyCompletion.

编译器,在实例方法前,还接受 GetAwaiter 扩展方法。在这种情况下,无扩展名的观测规定扩展:

The compiler, on top of instance methods, also accepts GetAwaiter extensions methods. In this case Reactive Extensions's Observable provides that extension:

public static AsyncSubject<TSource> GetAwaiter<TSource>(this IObservable<TSource> source);

例如,这是我们如何能够使awaitable字符串(其中编译,但显然不会实际工作):

For example, this is how we can make strings awaitable (which compiles but obviously won't actually work):

public static Awaiter GetAwaiter(this string s)
{
    throw new NotImplementedException();
}
public abstract class Awaiter : INotifyCompletion
{
    public abstract bool IsCompleted { get; }
    public abstract void GetResult();
    public abstract void OnCompleted(Action continuation);
}

await "bar";
 
精彩推荐
图片推荐