繁体   English   中英

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

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

我有以下服务方法,

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;
    }
}

问题是我不想在_imageService.DeleteAllAsync等待,因为它需要一些时间才能完成,所以我可以在不等待的情况下传输执行。 _imageService.DeleteAllAsync是否存在不使用await的_imageService.DeleteAllAsync

正确的方法是将此消息发送到消息队列,以便可以由外部进程异步处理(无需等待)。 这样,它还可以保证DeleteAllAsync在失败时被调用或重试。

这是我在这个主题上写的文章的无耻插件

暂无
暂无

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

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