【发布时间】:2013-02-14 00:21:36
【问题描述】:
我有以下代码,但经过的毫秒数似乎不准确:
public async Task<ActionResult> Index()
{
try
{
var connString = RoleEnvironment.IsEmulated
? ConfigurationManager.ConnectionStrings["Poc"].ConnectionString
: ConfigurationManager.ConnectionStrings["PocVm"].ConnectionString;
var repository = new AccountRepository(connString);
var stopWatch = new Stopwatch();
stopWatch.Start();
var accounts = await repository.GetAll();
stopWatch.Stop();
ViewBag.Accounts = accounts;
ViewBag.VmStatus = stopWatch.ElapsedMilliseconds;
}
catch (Exception e)
{
blah blah blah...
}
return View();
}
这看起来是正确的还是我错过了一些非常明显的东西?
【问题讨论】:
-
我觉得没问题。你为什么断定它不准确?仅供参考,使用这种技术无法准确测量非常短的时间;查看
Stopwatch.Frequency字段。 -
我也觉得不错。刚刚做了一个小测试(dotnetfiddle.net/wLzfor),看看 async 是否出于某种原因摆弄了秒表,但事实并非如此。
-
(即使使用
ConfigureAwait(false)也能工作)
标签: c# asynchronous