在任务完成之前等待返回

时间:2017-04-20 08:53:58

标签: c# asynchronous lambda async-await task

我有这个异步代码

private async Task<InvalidInvoiceViewModel> ValidateInsertedDataInRawTables(string uploadPath, long batchId)
    {
        var direcotryPath = Path.Combine(uploadPath, batchId.ToString());
        var allFiles = Directory.GetFiles(direcotryPath);
        var invoiceRawList = new List<InvoiceRaw>();
        var invoiceDetailRawList = new List<InvoiceDetailRaw>();
        await Task.Run(() => allFiles.ToList().ForEach(async file => 
        {
            var fileName = Path.GetFileNameWithoutExtension(file);
            switch (fileName)
            {
                case "invoice":
                    invoiceRawList = await ValidateInsertedDataInRawTablesForInvoiceAsync(batchId);
                    break;
                case "invoicedetails":
                    invoiceDetailRawList = await ValidateInsertedDataInRawTablesForInvoiceDetailAsync(batchId);
                    break;
            }
        }));
        var invalidInvoiceViewModel = new InvalidInvoiceViewModel
        {
            InvoiceRawList = invoiceRawList ,
            InvoiceDetailRawList = invoiceDetailRawList 
        };
        return invalidInvoiceViewModel;
    }

但是会发生什么,我总是在InvoiceRawListInvoiceDetailRawList得到0分。我发现该方法在异步lambda完成其处理之前返回。 (在chrome控制台中,结果显示在网络选项卡中,但ValidateInsertedDataInRawTablesForInvoiceAsync中的调试点在此之后被点击)。我正在等待我为这个lambda创建的Task.Run。我在这做错了什么?

评论后更新

如果我这样做,它就不会编译,我把它改为......

private async Task<InvalidInvoiceViewModel> ValidateInsertedDataInRawTables(string uploadPath, long batchId)
    {
        var direcotryPath = Path.Combine(uploadPath, batchId.ToString());
        var allFiles = Directory.GetFiles(direcotryPath);
        var invoiceRawList = new List<InvoiceRaw>();
        var invoiceDetailRawList = new List<InvoiceDetailRaw>();
        await Task.Run(async () => await allFiles.ToList().ForEach(async file => 
        {
            var fileName = Path.GetFileNameWithoutExtension(file);
            switch (fileName)
            {
                case "invoice":
                    invoiceRawList = await ValidateInsertedDataInRawTablesForInvoiceAsync(batchId);
                    break;
                case "invoicedetails":
                    invoiceDetailRawList = await ValidateInsertedDataInRawTablesForInvoiceDetailAsync(batchId);
                    break;
            }
        }));
        var invalidInvoiceViewModel = new InvalidInvoiceViewModel
        {
            InvoiceRawList = invoiceRawList ,
            InvoiceDetailRawList = invoiceDetailRawList 
        };
        return invalidInvoiceViewModel;
    }

并说不能等待无效。我知道这意味着什么,但那我该如何解决呢? List.ForEach是一种void方法。

2 个答案:

答案 0 :(得分:2)

你真的想要等待所有内部任务完成。

在这种情况下,

Task.WhenAll是你的朋友。

private async Task<InvalidInvoiceViewModel> ValidateInsertedDataInRawTables(string uploadPath, long batchId)
{
    var direcotryPath = Path.Combine(uploadPath, batchId.ToString());
    var allFiles = Directory.GetFiles(direcotryPath);
    var invoiceRawList = new List<InvoiceRaw>();
    var invoiceDetailRawList = new List<InvoiceDetailRaw>();

    List<Task> tasks = allFiles.Select(file => Task.Run(async () =>     
    {
        var fileName = Path.GetFileNameWithoutExtension(file);
        switch (fileName)
        {
            case "invoice":
                invoiceRawList = await ValidateInsertedDataInRawTablesForInvoiceAsync(batchId);
                break;
            case "invoicedetails":
                invoiceDetailRawList = await ValidateInsertedDataInRawTablesForInvoiceDetailAsync(batchId);
                break;
        }
    })).ToList();

    await Task.WhenAll(tasks);

    var invalidInvoiceViewModel = new InvalidInvoiceViewModel
    {
        InvoiceRawList = invoiceRawList,
        InvoiceDetailRawList = invoiceDetailRawList
    };
    return invalidInvoiceViewModel;
}

答案 1 :(得分:0)

如果要在其中使用等待,则必须使用常规的foreach循环。

    private async Task<InvalidInvoiceViewModel> ValidateInsertedDataInRawTables(string uploadPath, long batchId)
    {
        var direcotryPath = Path.Combine(uploadPath, batchId.ToString());
        var allFiles = Directory.GetFiles(direcotryPath);
        var invoiceRawList = new List<InvoiceRaw>();
        var invoiceDetailRawList = new List<InvoiceDetailRaw>();
        await Task.Run(async () =>
        {
            foreach(var file in allFiles)
            {
                var fileName = Path.GetFileNameWithoutExtension(file);
                switch (fileName)
                {
                    case "invoice":
                        invoiceRawList = await ValidateInsertedDataInRawTablesForInvoiceAsync(batchId);
                        break;
                    case "invoicedetails":
                        invoiceDetailRawList = await ValidateInsertedDataInRawTablesForInvoiceDetailAsync(batchId);
                        break;
                }
            }
        });
        var invalidInvoiceViewModel = new InvalidInvoiceViewModel
        {
            InvoiceRawList = invoiceRawList,
            InvoiceDetailRawList = invoiceDetailRawList
        };
        return invalidInvoiceViewModel;
    }

顺便说一句,这段代码没有多大意义,你创建invoiceRawListinvoiceDetailRawList只是为了在循环的每次迭代中替换它们。