- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑以下接口(interface):
public interface IProvider
{
Task<bool> Contains(string key);
}
这是满足 Visual Studio 的实现
public Task<bool> Contains(string key)
{
return Task.FromResult(false);
}
这个实现写起来很方便并且似乎可以达到同样的效果:
public async Task<bool> Contains(string key)
{
return false;
}
但是,Visual Studio 发出嘶嘶声并坚持:
This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await TaskEx.Run(...)' to do CPU-bound work on a background thread.
我很乐意忽略该警告并避免使用 Task.FromResult(...)
。
使用后一种选择是否有任何负面影响?
最佳答案
“嘶嘶作响”的原因是编译器需要做很多的工作来呈现一个以所有预期的正确方式工作的任务,你可以看到by compiling and decompiling it
Task.FromResult
更干净,但可能仍有开销 - IIRC 在某些情况下 Task.FromResult
在这里可能会高效工作(每次返回相同的对象),但我不会依赖它。
有两种实用可靠的方法:
Task<bool>
每次结果ValueTask<bool>
- 如果您经常同步返回,这在这里似乎很理想即
private readonly static Task<bool> s_False = Task.FromResult(false);
public Task<bool> Contains(string key, string scope)
{
return s_False ;
}
或
public ValueTask<bool> Contains(string key, string scope)
{
return new ValueTask<bool>(false);
}
注意:在这种情况下,第二个可能是不可能的,因为您没有定义接口(interface)。但是:如果您正在设计一个需要允许异步使用但实际上可能是同步的界面:考虑使用 ValueTask<T>
作为交换类型,而不是 Task<T>
.
生成的 C#:
public async System.Threading.Tasks.Task<bool> Contains(string key, string scope)
{
return false;
}
是这样的:
[StructLayout(LayoutKind.Auto)]
[CompilerGenerated]
private struct <Contains>d__0 : IAsyncStateMachine
{
public int <>1__state;
public AsyncTaskMethodBuilder<bool> <>t__builder;
private void MoveNext()
{
bool result;
try
{
result = false;
}
catch (Exception exception)
{
<>1__state = -2;
<>t__builder.SetException(exception);
return;
}
<>1__state = -2;
<>t__builder.SetResult(result);
}
void IAsyncStateMachine.MoveNext()
{
//ILSpy generated this explicit interface implementation from .override directive in MoveNext
this.MoveNext();
}
[DebuggerHidden]
private void SetStateMachine(IAsyncStateMachine stateMachine)
{
<>t__builder.SetStateMachine(stateMachine);
}
void IAsyncStateMachine.SetStateMachine(IAsyncStateMachine stateMachine)
{
//ILSpy generated this explicit interface implementation from .override directive in SetStateMachine
this.SetStateMachine(stateMachine);
}
}
[AsyncStateMachine(typeof(<Contains>d__0))]
public Task<bool> Contains(string key, string scope)
{
<Contains>d__0 stateMachine = default(<Contains>d__0);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<bool>.Create();
stateMachine.<>1__state = -1;
AsyncTaskMethodBuilder<bool> <>t__builder = stateMachine.<>t__builder;
<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
关于c# - 没有等待的异步方法与 Task.FromResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52146203/
我们的代码库(.NET Standard 2.0 库)中有以下方法: public Task GetDefaultTask() { return Task.FromResult(default
考虑以下接口(interface): public interface IProvider { Task Contains(string key); } 这是满足 Visual Studio
我对 async 有点陌生用 C# 编码, 并且遇到了一些我必须实现的方法(来自接口(interface)或继承服务的一部分,或者只是建议的默认约定)被标记为 async 的地方期待一个Task返回。
我有一项服务可以说, public interface ISomeService { Task DoSomeExpensiveCheckAsync(string parameter); } 我
我有这个简单的代码,可以异步启动方法。它使用TCS以便使用Task包装代码。 Task DoWork() { var source = new TaskCompletionSource ();
我使用这种形式的代码在基线上工作: Var result = await Task.FromResult(_someObject.SomeNonAsyncMethod(someParameterObj
我使用这种形式的代码在基线上工作: Var result = await Task.FromResult(_someObject.SomeNonAsyncMethod(someParameterObj
不确定我对异步等待工作原理的理解是否搞砸了,但这就是我遇到的问题。考虑一个人为的例子 此代码块 UI public async void LoginButtonClicked() { //creat
等待端点可用性的同步 repo.GetCategories 调用有什么好处? 我的第一个想法是这毫无意义。但我不确定。 [HttpGet] public async Task Get(string c
这个问题在这里已经有了答案: Why is Task not co-variant? (4 个答案) 关闭 4 年前。 我有以下程序: async Task Main() { IList m
考虑这段代码: class Program { public async Task ReturnTask(bool value) { if (!value)
我正在浏览 tutorial在 asp.net vnext 系列中。我在 tutorial 中遇到了一些东西这没有多大意义: using System.Linq; using Microsoft.As
这个问题在这里已经有了答案: Await or Task.FromResult (3 个答案) 关闭 4 年前。 我们有一个返回类型为 async Task<> 的遗留方法.它当前的实现需要查询数据
因此,我一直在将 IServiceScopeFactory 注入(inject)我的页面,以便在通过 EF Core 获取数据时使用它的作用域。但是昨天我偶然发现有人在调用数据库时使用 Task.Fr
我最近遇到了很多情况,其中 async 方法同步执行,但无论如何都会返回一个任务,因此可以等待它们,例如 public virtual Task CreateAsync(TUser user) {
关于的功能和含义有什么区别 TaskCompletionSource + SetResult vs Task + FromResult 在 SendAsync 方法中? protected overr
我正在将一些遗留的 C# 类重写为 F#。其中一个类使用 this.SendAsync(identityMessage) 的单一方法实现了 IIdentityMessageService 在 C# 代
使用 Entity Framework 中 System.Data.Entity 提供的异步方法与使用 Task.FromResult 包装相同的非异步方法之间有什么区别吗? 例如: priv
我正在尝试找出使用异步等待通过 Entity Framework 选择数据的最佳方式。在下面的代码中,我有两个选项。 第一个使用 Task.FromResult。 第二个选项尝试使用 async aw
在 C# 和 TPL ( Task Parallel Library ) 中,Task 类表示正在进行的工作,它会产生 T 类型的值。 我想知道 Task.FromResult 需要什么方法? 即:在
我是一名优秀的程序员,十分优秀!