Because when you use await
you are actually "awaiting" the result, so if you block on ExecuteBatch
, it won't end till ExcecuteBatchAsync
ends.
In the other hand, ExecuteBatch2
does not "await" anything. Even if you block on the ExecuteBatch2
response, the ExecuteBatchAsync
operation is launched in parallel and ExecuteBatch2
ends, despite of the task launched by ExecuteBatchAsync
is still running.
UPDATE:
1| var stopwatch = Stopwatch.StartNew();
2| await cloudTable.ExecuteBatchAsync(tableBatchOperation);
3| stopwatch.Stop();
4| log.WriteLine("Committed " + tableBatchOperation.Count + " records in " + stopwatch.Elapsed.TotalSeconds + " seconds.");
In the first method, because you are awaiting, you won't get to line #3 until line #2 ends.
However in your second method:
1| var stopwatch = Stopwatch.StartNew();
2| cloudTable.ExecuteBatchAsync(tableBatchOperation);
3| stopwatch.Stop();
4| log.WriteLine("Committed " + tableBatchOperation.Count + " records " + " in " + stopwatch.Elapsed.TotalSeconds + " seconds.");
You will get from line #2 to line #3 immediately, because ExecuteBatchAsync
is happening in parallel, nothing is making the thread block for a result. If you do cloudTable.ExecuteBatchAsync(tableBatchOperation).Wait()
you will probably get the same result than in the first method.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…