如果我的接口必须返回 Task,那么实现无操作的最佳方法是什么? [英] If my interface must return Task what is the best way to have a no-operation implementation?

查看:17
本文介绍了如果我的接口必须返回 Task,那么实现无操作的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,由于接口的原因,LazyBar 类必须从它的方法中返回一个任务(并且为了参数不能改变).如果 LazyBar 的实现不寻常,因为它碰巧快速且同步地运行 - 从该方法返回 No-Operation 任务的最佳方法是什么?

In the code below, due to the interface, the class LazyBar must return a task from its method (and for argument's sake can't be changed). If LazyBars 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?

我已经使用了下面的 Task.Delay(0),但是我想知道如果该函数被称为 lot 是否有任何性能副作用(为了论证,每秒说数百次):

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 argument's sake, say hundreds of times a second):

  • 这个句法糖是否可以分解成一些大的东西?
  • 它是否开始阻塞我的应用程序的线程池?
  • 编译器是否足以以不同的方式处理 Delay(0)?
  • return Task.Run(() => { }); 会有什么不同吗?
  • 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?

有更好的方法吗?

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();
        }
    }
}

推荐答案

今天,我建议使用 Task.CompletedTask 来完成这个.

Today, I would recommend using Task.CompletedTask to accomplish this.

.net 4.6 之前:

Pre .net 4.6:

使用 Task.FromResult(0)Task.FromResult(null) 将比创建一个 Task 产生更少的开销一个无操作表达式.创建具有预先确定结果的 Task 时,不涉及调度开销.

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.

这篇关于如果我的接口必须返回 Task,那么实现无操作的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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