什么是包装一个任务,任务和LT的最佳途径; TResult> [英] What's the best way to wrap a Task as a Task<TResult>

查看:175
本文介绍了什么是包装一个任务,任务和LT的最佳途径; TResult>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些异步辅助方法,我有API来支持工作任务< T> 。重新使用code,我想在工作基于API来包装给定任务作为任务< T> 并调用通过对任务< T> API

I am writing some async helper methods and I have APIs to support both Task and Task<T>. To re-use code, I'd like the Task-based API to wrap the given task as a Task<T> and just call through to the Task<T> API.

我能做到这一点的方法之一是:

One way I can do this is:

private static async Task<bool> Convert(this Task @this)
{
    await @this.ConfigureAwait(false);
    return false;
}

不过,我想知道:有没有更好的/内置的方式做到这一点。

However, I'm wondering: is there there is a better/builtin way to do this?

推荐答案

有没有说正是这一点现有的任务方法,没有。你的方法是好的,而且很可能一样简单,你就可以得到的。

There is no existing Task method that does exactly this, no. Your method is fine, and is likely about as simple as you'll be able to get.

用任何其他方法实施正确的错误传播/取消语义是看似困难的。

Implementing the proper error propagating/cancellation semantics using any other method is deceptively hard.

这篇关于什么是包装一个任务,任务和LT的最佳途径; TResult&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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