如果我的界面必须返回任务是什么有一个无操作实现的最佳途径? [英] If my interface must return Task what is the best way to have a no-operation implementation?
问题描述
在code以下时,由于接口,类 LazyBar
必须返回一个任务,从它的方法(和参数的缘故不能更改) 。如果 LazyBar
的实现是不寻常的,因为它发生在快速同步运行 - 什么是从该方法返回一个空操作任务的最佳方式
我已经用 Task.Delay(0)
下面,不过,我想知道这是否有任何性能的副作用,如果该功能被称为很多的(为了讨论各种情形,说几百次,第二次):
- 这是否语法糖未风大的东西?
- 是否开始堵塞我的应用程序的线程池?
- 是编译器切割刀足以应对
延迟(0)
不同? - 将为
返回Task.Run(()=> {});
有什么不同
有没有更好的办法?
使用System.Threading.Tasks;
命名空间MyAsyncTest
{
内部接口IFooFace
{
任务WillBeLongRunningAsyncInTheMajorityOfImplementations();
}
///<总结>
///的实现,不像大多数情况下,不会有长期运行
///操作WillBeLongRunningAsyncInTheMajorityOfImplementations
///< /总结>
内部类LazyBar:IFooFace
{
#地区IFooFace会员
公共任务WillBeLongRunningAsyncInTheMajorityOfImplementations()
{
//首先,做一些真正快速
变种X = 1;
//不能返回空在这里!难道Task.Delay(0)有什么性能方面的考虑?
//它是一个真正的无操作,或者如果我叫这个有很多,是否会产生不利影响
//底层线程池?更好的办法?
返回Task.Delay(0);
//什么不同呢?
//返回Task.Run(()=> {});
//如果我的任务返回的东西,我会做:
//返回Task.FromResult<诠释>(12345);
}
#endregion
}
内部类节目
{
私有静态无效的主要(字串[] args)
{
测试();
}
私有静态异步无效测试()
{
IFooFace富= FactoryCreate();
等待foo.WillBeLongRunningAsyncInTheMajorityOfImplementations();
返回;
}
私有静态IFooFace FactoryCreate()
{
返回新LazyBar();
}
}
}
使用 Task.FromResult(0)
或 Task.FromResult<对象> (空)
将承担比创建一个工作
用无操作EX pression开销更少。当创建一个工作
与结果pre-确定的,没有调度开销参与。
In the code below, due to the interface, the class LazyBar
must return a task from it's method (and for arguments sake can't be changed). If LazyBar
s implementation is unusual in that it happens to run quickly and synchronously - what is the best way to return a No-Operation task from the method?
I have gone with Task.Delay(0)
below, however I would like to know if this has any performance side-effects if the function is called a lot (for arguments sake, say hundreds of times a second):
- Does this syntactic sugar un-wind to something big?
- Does it start clogging up my application's thread pool?
- Is the compiler cleaver enough to deal with
Delay(0)
differently? - Would
return Task.Run(() => { });
be any different?
Is there a better way?
using System.Threading.Tasks;
namespace MyAsyncTest
{
internal interface IFooFace
{
Task WillBeLongRunningAsyncInTheMajorityOfImplementations();
}
/// <summary>
/// An implementation, that unlike most cases, will not have a long-running
/// operation in 'WillBeLongRunningAsyncInTheMajorityOfImplementations'
/// </summary>
internal class LazyBar : IFooFace
{
#region IFooFace Members
public Task WillBeLongRunningAsyncInTheMajorityOfImplementations()
{
// First, do something really quick
var x = 1;
// Can't return 'null' here! Does 'Task.Delay(0)' have any performance considerations?
// Is it a real no-op, or if I call this a lot, will it adversely affect the
// underlying thread-pool? Better way?
return Task.Delay(0);
// Any different?
// return Task.Run(() => { });
// If my task returned something, I would do:
// return Task.FromResult<int>(12345);
}
#endregion
}
internal class Program
{
private static void Main(string[] args)
{
Test();
}
private static async void Test()
{
IFooFace foo = FactoryCreate();
await foo.WillBeLongRunningAsyncInTheMajorityOfImplementations();
return;
}
private static IFooFace FactoryCreate()
{
return new LazyBar();
}
}
}
Using Task.FromResult(0)
or Task.FromResult<object>(null)
will incur less overhead than creating a Task
with a no-op expression. When creating a Task
with a result pre-determined, there is no scheduling overhead involved.
这篇关于如果我的界面必须返回任务是什么有一个无操作实现的最佳途径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!