使用秒表类测量C#/异步数据访问
我有以下代码,似乎经过的毫秒不准确:使用秒表类测量C#/异步数据访问,c#,asynchronous,C#,Asynchronous,我有以下代码,似乎经过的毫秒不准确: public async Task<ActionResult> Index() { try { var connString = RoleEnvironment.IsEmulated ? ConfigurationManager.ConnectionStrings["Poc"].ConnectionString
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();
}
公共异步任务索引()
{
尝试
{
var connString=RoleEnvironment.IsEmulated
?配置管理器.连接字符串[“Poc”].连接字符串
:ConfigurationManager.ConnectionString[“PocVm”]。ConnectionString;
var repository=新的AccountRepository(connString);
var stopWatch=新秒表();
秒表。开始();
var accounts=await repository.GetAll();
秒表;
ViewBag.Accounts=账户;
ViewBag.VmStatus=stopWatch.ElapsedMilliseconds;
}
捕获(例外e)
{
废话废话。。。
}
返回视图();
}
这看起来是正确的还是我遗漏了一些非常明显的东西?我觉得这很好 如果Repository.GetAll方法不是异步的,则可能会出现一个错误,希望它具有如下签名:
public async Task<IEnumerable<Account>> GetAll();
公共异步任务GetAll();
我觉得还可以。为什么你认为这是不准确的?仅供参考,使用此技术无法准确测量非常短的计时;查看Stopwatch.Frequency
字段。我看也不错。只是做了一个小测试(),看看async是否出于某种原因摆弄了秒表,但它没有(即使使用ConfigureAwait(false)
)也能工作),我认为如果没有async方法签名,它是不会编译的