简体   繁体   English

ContinueWith不处理异步回调吗?

[英]Does ContinueWith not handle Async callbacks?

I saw Stephen Cleary's blog about Startnew being dangerous and how continuewith is also dangerous . 我看到斯蒂芬克莱里的博客关于Startnew是危险的继续是多么危险 I wanted to use it here to avoid having to write a try finally just to make a call to NSubstitute in the case of an exception. 我想在这里使用它来避免最后只是为了在异常情况下调用NSubstitute而尝试编写。 I found the Test passed when it shouldn't have and then noticed that the exception was thrown but it didn't make it out of the test to signal nunit. 我发现测试通过了它应该没有,然后注意到异常被抛出但它没有使它超出测试信号nunit。

Does ContinueWith act similarly to Task.Startnew with respect to async functions? 对于异步函数,ContinueWith是否与Task.Startnew类似? I noticed this simplified equivalent will not throw the inner exception in Nunit 3. 我注意到这个简化的等价物不会在Nunit 3中抛出内部异常。

[Test]
public async Task SimpleTest()
{
    await Task.Delay(10).ContinueWith( async t =>
    {
        await Task.Run(()=>{throw new Exception();});
    });
}

ContinueWith does not understand async lambdas. ContinueWith不理解async lambdas。 You would need to use Unwrap in addition to passing a task scheduler. 除了传递任务调度程序之外,您还需要使用Unwrap

I wanted to use it here to avoid having to write a try finally just to make a call to NSubstitute in the case of an exception. 我想在这里使用它来避免最后只是为了在异常情况下调用NSubstitute而尝试编写。

I don't understand this requirement. 我不明白这个要求。 Why wouldn't this work? 为什么这不起作用?

await Task.Delay(10);
await Task.Run(() => { throw new Exception(); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM