简体   繁体   English

等待一个任务而不是其他任务可以吗?

[英]Is it fine to await for one Task but not for other?

I have a following service method, 我有以下服务方法,

public async Task<IList<ProductsImage>> InsertAsync(BaseProduct product, Dictionary<string, Stream> images)
{
    try
    {
        if (images.Count > 0)
        {
            await _imageService.SaveAllAsync(images);
        }
        return await _repository.InsertAsync(product);
    }
    catch // Delete the files if there is an error from db
    {
        if (images.Count > 0)
        {
            var paths = images.Select(i => i.Key).ToList();
            _imageService.DeleteAllAsync(paths);                        
        }
        throw;
    }
}

The problem is that I don't want to await at _imageService.DeleteAllAsync because it takes some time to finish, so that I can transfer my execution without waiting. 问题是我不想在_imageService.DeleteAllAsync等待,因为它需要一些时间才能完成,所以我可以在不等待的情况下传输执行。 Is there any danger of not using await in _imageService.DeleteAllAsync _imageService.DeleteAllAsync是否存在不使用await的_imageService.DeleteAllAsync

The right way is to send this message to a message queue so it can be processed asynchronously (without await) by an outside process. 正确的方法是将此消息发送到消息队列,以便可以由外部进程异步处理(无需等待)。 This way it will also guarantee that DeleteAllAsync will get called or retried if it fails. 这样,它还可以保证DeleteAllAsync在失败时被调用或重试。

Here is my shameless plug for the article I wrote on this subject. 这是我在这个主题上写的文章的无耻插件

暂无
暂无

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

相关问题 在其他“异步”方法中“等待”方法是否合适 - Is it fine to “await” methods inside other “async” methods 有没有一种方法可以触发2个任务,等待一个任务完成(不取消另一个任务),然后设置一个返回结果的时间 - Is there a way to trigger 2 tasks, await for one to complete (without cancelling the other task) and then set a time before returning result 如何在两个不同的线程上等待一项任务? - How to await for one task on two different threads? 为什么在 Ctrl+C 我的进程突然停止在“任务<T> .Result”,但适用于“await Task”<T> ”? - Why on Ctrl+C my process suddenly stopped on “Task<T>.Result”, but works fine with “await Task<T>”? 任务异步/等待无法在WPF中工作,因为它在其他情况下也可以工作 - Task async/await not working from WPF as it works in other scenarios 一个Task的Output输入到另一个Task - Output of one Task input to other Task 一个DateTime可以很好地解析,但另一个不是吗? - One DateTime parses fine but the other doesn't? 代理的生成在一种环境中工作正常,但在另一种环境中却行不通? - Generation of proxy works fine in one environment but not in the other? Async/await, Task Parallel 对于在调试时运行 FINE 但没有运行失败的复杂进程 - Async/await, Task Parallel For complex process running FINE on while debugging but failed to run without await Task 和 await Func 的区别<task> ()</task> - Difference between await Task and await Func<Task>()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM