Unity3D中的Awaiter实现有什么问题?

时间:2019-04-04 12:39:50

标签: c# unity3d async-await task

所以我试图制作一个简单的等待结构,以允许我返回布尔值:

// define a special container for returning results
public struct BoolResult {
    public bool Value;
    public BoolAwaiter GetAwaiter () {
        return new BoolAwaiter(Value);
    }
}

// make the interface task-like
public readonly struct BoolAwaiter : INotifyCompletion {
    private readonly bool _Input;

    // wrap the async operation
    public BoolAwaiter (bool value) {
        _Input = value;
    }

    // is task already done (yes)
    public bool IsCompleted {
        get { return true; }
    }

    // wait until task is done (never called)
    public void OnCompleted (Action continuation) => continuation?.Invoke();

    // return the result
    public bool GetResult () {
        return _Input;
    }
}

我正在像这样使用它

private async BoolResult LoadAssets (string label) {
    //
    // some await asyncFunction here
    //

    // then at the end
    return new BoolResult { Value = true };
}

但是我仍然收到此编译错误:

  

error CS1983: The return type of an async method must be void, Task or Task<T>

我以为我的BoolResult已经像任务一样?这是什么问题?

0 个答案:

没有答案