[英]Cannot implicitly convert type bool to System.Threading.Tasks.Task<bool>
I'm tying to follow the async await structure as described on msdn here 我绑遵循异步的await结构MSDN上描述这里
So I have an async
task: 所以我有一个
async
任务:
public async void Starter()
{
CC = new CustomClass();
Task<bool> newTask = CC.MethodThatReturnsBool();
// DO SOME VISUAL THINGS
bool b = await newTask;
}
}
But I'm getting a compile-stopping error around the CC.MethodThatReturnsBool();
但是我在
CC.MethodThatReturnsBool();
周围遇到了编译停止错误CC.MethodThatReturnsBool();
as shown by my question header. 如我的问题标题所示。
How do I get my bool in the right format? 如何以正确的格式获取bool? I tried randomly casting (Task) in front of the erroneous method call but it just removed the "implicitly" from my error message!
我尝试在错误的方法调用之前随机强制转换(任务),但它只是从错误消息中删除了“隐式”!
I'm using: 我正在使用:
VS Community 2017 and .NET Framework 4.7 VS社区2017和.NET Framework 4.7
According to the error, this returns a bool
: 根据错误,这将返回
bool
:
CC.MethodThatReturnsBool()
So you'd store it as such: 因此,您应将其存储为:
bool newBool = CC.MethodThatReturnsBool();
Which, it would seem, means that there are no asynchronous operations to be awaited. 看来,这意味着没有等待异步的操作。 Simplifying:
简化:
public void Starter()
{
CC = new CustomClass();
bool b = CC.MethodThatReturnsBool();
// do something with your bool?
}
It's not clear why you're expecting to await
anything, and there may indeed be more to this that you're not showing, but if the method you're calling is synchronous then there's no need to await it. 目前尚不清楚为什么期望
await
任何东西,而且确实可能还有更多没有显示的东西,但是如果您正在调用的方法是同步的,则无需等待。
Side note: async void
is a really bad idea . 旁注:
async void
是一个非常糟糕的主意 。 It only exists for compatibility with UI event handlers and such. 它仅用于与UI事件处理程序等兼容。 If you're writing anything that you control, don't use
async void
. 如果要编写自己控制的内容,请不要使用
async void
。
Edit: 编辑:
Based on your update to the question, it sounds like what's happening here is that you have a long-running synchronous process that you'd like to not block the UI thread? 根据您对该问题的更新, 听起来这里发生的事情是您有一个长时间运行的同步进程,不想阻止UI线程? You can wrap something in a
Task
, perhaps something like this: 您可以在
Task
包装一些东西,也许是这样的:
bool b = await Task.Run(() => new CustomClass().MethodThatReturnsBool());
So perhaps overall you're looking for something more like this (untested)?: 因此,也许总的来说您正在寻找更像这样的东西(未经测试)?:
public async Task Starter()
{
Task<bool> newTask = Task.Run(() => new CustomClass().MethodThatReturnsBool());
// DO SOME VISUAL THINGS
bool b = await newTask;
// do something with your bool?
}
To elaborate on a previous point, note that the overall method is now async Task
instead of async void
. 要详细说明前一点,请注意,整个方法现在是
async Task
而不是async void
。 This makes Starter
itself awaitable, as void otherwise would not be. 这使得
Starter
本身就可以等待,否则就没有空。
Ideally of course, MethodThatReturnsBool
should itself be async
if it is internally performing various I/O bound operations which cause its delay. 当然, 理想情况下 ,如果
MethodThatReturnsBool
在内部执行各种导致延迟的I / O绑定操作,则它本身应该是async
。 (As implied in your comment on the question above.) That may involve refactoring elsewhere in the system, but in the long run is the preferable approach. (如您对上述问题的评论所暗示。)这可能涉及重构系统中的其他位置,但从长远来看,是首选的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.