异步等待顶级处理

时间:2015-07-17 22:21:47

标签: c# .net asynchronous async-await

我需要大约10个SQL表来提取数据,处理,然后将这些数据插入到另一个表中。我不想并行完成所有这些,但我想利用所有的IO时间。

我有一个功能:

    public async Task PromoteAsync()
    {
        try
        {
            using (HTTransDB transactions = new HTTransDB())
            using (ZImportMaster importMaster = new ZImportMaster())
            using (ZMasterAddress masterAddress = new ZMasterAddress())
            {
                // Select out non error rows
                DataTable dt = await HTTransDB.GetValidRowsAsync(this.TableConfig.BatchID, this.TableConfig.SourceTableName);


                // Do stuff

                // insert data
                using (HTTransDB transactionsDB = new HTTransDB())
                {
                    int result = await transactionDB.BulkCopyAsync(dt);
                }
            }
        }
        catch (Exception e)
        {
            // log exception and re-throw
        }
    }

我在每个表的foreach循环中运行此函数:

    public async Task PromoteLoad()
    {
        // Load each batch
        foreach (var table in this.Tables)
        {
            await table.PromoteAsync();
        }
    }

我的问题是,如何从非Async方法调用此函数? Task.Run?我不想启动新线程,我只是希望进程在等待IO时继续运行。

1 个答案:

答案 0 :(得分:0)

只需调用方法即可启动异步工作:

var promoteTask = PromoteAsync();

请注意,返回的任务表示该方法的执行。

相关问题