gpt4 book ai didi

c# - async Task if 条件下的方法调用

转载 作者:太空狗 更新时间:2023-10-29 21:21:42 26 4
gpt4 key购买 nike

我想知道以下代码是在执行主线程之前等待异步方法完成,还是仅在有条件时继续主线程并认为方法返回为 false。

public async Task<bool> SomeMethod
{
if(await AsyncMethod(param))
{
//Do something
}
}

...

异步方法定义为:

public async Task<bool> AsyncMethod(SomeClass param)
{
//Do something
}

最佳答案

I would like to know if the following code will wait for the async method to complete before executing the main thread or will just continue the main thread if condition and consider the method return as false.

都没有。

await 是一种“异步等待”。换句话说,方法 会等待,但线程 不会。

当您的方法遇到 await(假设它实际上有一些等待要做)时,它会立即将未完成的任务返回给 SomeMethod 的调用者。线程继续执行它想做的任何事情。稍后,当等待的任务完成时,SomeMethod 将继续执行。当 SomeMethod 完成时,它之前返回的任务将完成。

我将详细介绍我的 blog post on the subject .

关于c# - async Task<bool> if 条件下的方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35873072/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com