Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用秒表类测量C#/异步数据访问_C#_Asynchronous - Fatal编程技术网

使用秒表类测量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方法签名,它是不会编译的