问题
To increase my page performance I wanted to run a few queries asynchronously.
I upgraded to EF6 because it natively supports this feature by exposing async methods.
I couldn't get the queries to fire simultaneously so i boiled my code down to this simple example :
var sw = new Stopwatch();
sw.Start();
var dummy = context.Set<CA_Event_Person>().Take(200).ToListAsync();
sw.Stop();
Logger.Debug("attempt nr 1 : " + sw.ElapsedMilliseconds);
var result = await dummy;
My exception was that the stopwatch with instantly stop since i'm doing the await at a later point.
My logger however says that 5000 milliseconds have passed on the stopwatch meaning the call was performed synchronous instead of asynchronous.
Anyone got some insight in the why or what i could be doing wrong?
回答1:
Anyone got some insight in the why or what I could be doing wrong?
Is this the first call into EF in the application? Could you be timing the one-off building of the internal representation of the EF model?
With a more complex EF model there is significant overhead on the first call. But this is a one off (per app domain) cost.
Better to time a number of calls (and then throw away the shortest and longest times).
来源:https://stackoverflow.com/questions/31287942/ef6-tolistasync-does-not-run-async-but-blocks-the-thread