C# 创建完成的任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14244114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a completed Task
提问by Timothy Shields
I want to create a completed Task
(not Task<T>
). Is there something built into .NET to do this?
我想创建一个完整的Task
(不是Task<T>
)。.NET 中是否有一些东西可以做到这一点?
A related question: Create a completed Task<T>
一个相关的问题: 创建一个完整的 Task<T>
采纳答案by i3arnon
The newest version of .Net (v4.6)is adding just that, a built-in Task.CompletedTask:
.Net (v4.6)的最新版本只是添加了一个内置的Task.CompletedTask:
Task completedTask = Task.CompletedTask;
That property is implemented as a no-lock singleton so you would almostalways be using the same completed task.
该属性实现为无锁单例,因此您几乎总是使用相同的已完成任务。
回答by Reed Copsey
You can use Task.FromResult(in .NET 4.5) to return a completed Task<T>
.
您可以使用Task.FromResult(在 .NET 4.5 中)返回一个已完成的Task<T>
.
If you need a non-generic Task
, you can always use Task.FromResult(0)
or similar, since Task<T>
is a subclass of Task
.
如果你需要一个非泛型的Task
,你总是可以使用Task.FromResult(0)
或类似的,因为它Task<T>
是Task
.
回答by Servy
Task<T>
is implicitly convertable to Task
, so just get a completed Task<T>
(with any T
and any value) and use that. You can use something like this to hide the fact that an actual result is there, somewhere.
Task<T>
可隐式转换为Task
,因此只需获取一个已完成的Task<T>
(具有任何T
和任何值)并使用它。您可以使用类似的方法来隐藏实际结果在某处的事实。
private static Task completedTask = Task.FromResult(false);
public static Task CompletedTask()
{
return completedTask;
}
Note that since we aren't exposing the result, and the task is always completed, we can cache a single task and reuse it.
请注意,由于我们不公开结果,并且任务始终完成,因此我们可以缓存单个任务并重用它。
If you're using .NET 4.0 and don't have FromResult
then you can create your own using TaskCompletionSource
:
如果您使用的是 .NET 4.0 并且没有,FromResult
那么您可以使用TaskCompletionSource
以下方法创建自己的:
public static Task<T> FromResult<T>(T value)
{
var tcs = new TaskCompletionSource<T>();
tcs.SetResult(value);
return tcs.Task;
}
回答by gzak
I would use Task.Delay(0)
. Internally, it returns a cached instance of a completed Task<T>
. This is exactly what the current answer suggest doing anyway, only now you don't have to cache an instance yourself, nor do you have any inelegant garbage values in your code.
我会使用Task.Delay(0)
. 在内部,它返回一个已完成的缓存实例Task<T>
。这正是当前答案建议做的事情,只是现在您不必自己缓存实例,代码中也没有任何不雅的垃圾值。
You might be thinking you can use Task.Yield()
instead, but it turns out the result of Task.Yield()
is nota subtype of Task
, whereas the result of Task.Delay(0)
is. That's one of the subtle differences between the two.
您可能认为您可以使用Task.Yield()
代替,但结果Task.Yield()
是不是的子类型Task
,而结果Task.Delay(0)
是。这是两者之间的细微差别之一。
回答by Alexander Logger
You can use Nito.AsyncEx.TaskConstants.Completedfrom a great library AsyncExfrom Stephen Cleary.
您可以使用Nito.AsyncEx.TaskConstants.Completed从一个伟大的图书馆AsyncEx从斯蒂芬·克利里。
回答by Richiban
My preferred method for doing this is to call Task.WhenAll()
with no arguments. The MSDN documentationstates that "If the supplied array/enumerable contains no tasks, the returned task will immediately transition to a RanToCompletion state before it's returned to the caller.". That sounds like what you want.
我的首选方法是Task.WhenAll()
不带参数调用。在MSDN文档指出:“如果所提供的阵列/枚举不包含任何任务,返回的任务将立即过渡到RanToCompletion状态之前它返回给调用者。” 这听起来像你想要的。
Update: I found the source over at Microsoft's Reference Source; there you can see that Task.WhenAll contains the following:
更新:我在Microsoft's Reference Source找到了源代码;在那里你可以看到 Task.WhenAll 包含以下内容:
return (tasks.Length == 0) ? // take shortcut if there are no tasks upon which to wait
Task.CompletedTask :
new WhenAllPromise(tasks);
So Task.CompletedTask is indeed internal, but it is exposed by calling WhenAll() with no arguments.
所以 Task.CompletedTask 确实是内部的,但它是通过不带参数调用 WhenAll() 来公开的。
回答by Dorus
How about:
怎么样:
#pragma warning disable 1998
public async Task emptyTask() {
}
#pragma warning restore 1998
You can leave out the warning suppression if you don't mind it.
如果您不介意,可以省略警告抑制。
回答by Icen
For .Net 4.6 and above use
对于 .Net 4.6 及以上使用
return Task.CompletedTask;
For lower version you can use
对于较低版本,您可以使用
return new Task(() => { });