C#异步任务等待VS等待
问题描述:
也有一些是被缠着我了一会儿,在我的代码我无法理解这种情况发生的东西。C#异步任务等待VS等待
我定义的工作流以从Facebook API
提取信息,基本上我有3个不同的async tasks
。
执行报告
private static async Task<List<DataTypes.ReportRun>> ExecuteMarketingReports(FacebookClient fb, List<DataTypes.Request.Batch> batch_requests)
{
//Do stuff - Make async POST requests to
//FB to execute reports on FB server side
}
Monitorize报告
private static async Task<List<DataTypes.AdReportRun>> MonitorizeMarketingReports(FacebookClient fb, List<DataTypes.ReportRun> tListReportRun)
{
//Do stuff -- Check if the reports are ready or not, and return a list with status
}
GetReportData
private static async Task GetReportData(FacebookClient fb, List<DataTypes.AdReportRun> tListReportRun, DateTime since, DateTime until, string breakdown)
{
//Do stuff - Gets report thata once the reports are finish and create the files
}
这是所有其它方法被调用
private static async Task PullInsightsData(FacebookClient fb, List<DataTypes.Request.Batch> batchRequests, DateTime since, DateTime until, string breakdown)
{
var tResult = new List<DataTypes.AdReportRun>();
int retry = 0;
List<DataTypes.AdReportRun> tReportCompleteList = new List<DataTypes.AdReportRun>();
List<DataTypes.AdReportRun> tReportIncompleteList = new List<DataTypes.AdReportRun>();
var report_ids = await ExecuteMarketingReports(fb, batchRequests);
Thread.Sleep(20000); // Waits 20 seconds before get the info.
do
{
/*Start monitorizing the reports*/
var tReport_info = await MonitorizeMarketingReports(fb, report_ids);
/*Get the reports that are complete*/
tReportCompleteList = tReport_info.Where(x => x.async_percent_completion == 100).ToList();
if (tReportCompleteList.Count > 0)
await GetReportData(fb, tReportCompleteList, since, until, breakdown);
tReportIncompleteList = tReport_info.Where(x => x.async_percent_completion < 100).ToList();
report_ids = (from x in tReportIncompleteList
select new DataTypes.ReportRun { report_run_id = x.id }).ToList();
var sleepTime = TimeSpan.FromSeconds(Math.Pow(2, retry + 1));
Thread.Sleep(sleepTime);
retry++;
} while (report_ids.Count > 0 && retry < 8);
}
我调用这个foreach
环路我的主要工作的主要任务,这是发生问题。
for (int i = 0; i < ActiveAdAccounts.Count; i = i + 50)
{
var AdAccountsSubList = ActiveAdAccounts.Skip(i).Take(50).ToList();
var batchRequests = ....
await PullInsightsData(fb, batchRequests, (DateTime)since, (DateTime)until, breakdown.Replace(",", "_"));
//tTaskList.Add(PullInsightsData(fb, batchRequests, (DateTime)since, (DateTime)until, breakdown.Replace(",", "_")));
}
//Task.WaitAll(tTaskList);
我不明白为什么foreach
循环简化版,继续使用await
控制台应用程序关闭突然,不应该await
“等待”直到任务完成,因此然后进行下一行码?
我已经解决了这个问题,把所有的任务到一个列表,并等待所有,但我想这个解释。
[编辑]问题被编辑创建一个最小的可重现的例子。
答
当您在方法中使用await
关键字时,它将被暂停并将控制权返回给方法的调用方,直到完成等待的方法中的工作。在控制台应用程序中,主线程将完成其工作,以便程序退出。
就拿下面的程序:
class Program
{
static void Main()
{
DoWork();
// Will exit immediately
}
static async Task DoWork()
{
await Task.Delay(10000);
// Should wait 10 seconds
}
}
这个程序将立即退出,因为主线程不await
的DoWork
异步方法。
的可能的复制[如何以及何时使用\'异步\'和\'\伺机'](http://stackoverflow.com/questions/14455293/how-and-when-to-use-async-and等待) –
如果重复,我道歉,我还没有找到其他有关我的问题。 – bmvr
请包括一个最小的,可重复的例子。你的'for' /'foreach'循环现在没有'await'。 “await”确实会暂停该方法,直到该任务完成。的 “早归” 最常见的原因是使用'StartNew'(使用'Task.Run'代替),'ContinueWith'(使用'await'代替),或'异步void'(使用'异步Task'代替)。 –