Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.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#_Asp.net Mvc - Fatal编程技术网

C# 秒表时间过零

C# 秒表时间过零,c#,asp.net-mvc,C#,Asp.net Mvc,在mvc中我有两个动作,一个是启动一个全局秒表,另一个是停止它,但不管经过多长时间,经过的时间总是0。这两个事件都是通过单击按钮触发的。我的怀疑是按钮的柱子在干扰我的时间,也许吧?如果是这样的话,有什么办法可以解决这个问题吗 public Stopwatch stopwatch = new Stopwatch(); public ActionResult Start() { stopwatch.Start(); return Re

在mvc中我有两个动作,一个是启动一个全局秒表,另一个是停止它,但不管经过多长时间,经过的时间总是0。这两个事件都是通过单击按钮触发的。我的怀疑是按钮的柱子在干扰我的时间,也许吧?如果是这样的话,有什么办法可以解决这个问题吗

public Stopwatch stopwatch = new Stopwatch();

public ActionResult Start()
        {
            stopwatch.Start();
            return RedirectToAction("Index");
        }

        public ActionResult Stop(int workid)
        {
            stopwatch.Stop();
            TimeSpan ts = stopwatch.Elapsed;
            int hours = ts.Hours;
            int mins = ts.Minutes;

            using (ZDevContext db = new ZDevContext())
            {
                DashboardHelper dashhelper = new DashboardHelper(db);
                dashhelper.RecordTimeSpent(workid, hours, mins);
            }

                return View("Index");
        }

它不是相同的秒表-控制器是为每个请求重新创建的。你需要把秒表放在某个地方

您可以在
静态字典
中持久化开始时间,该字典将
工作ID
映射到开始时间

static ConcurrentDictionary<int,DateTimeOffset> starts = new ConcurrentDictionary<int,DateTimeOffset>(); 

public ActionResult Start(int workId)
{
    starts.TryAdd(workId, DateTimeOffset.Now);
    return RedirectToAction("Index");
}

public ActionResult Stop(int workId)
{
    DateTimeOffset started = DateTimeOffset.MinValue;
    if (starts.TryGet(workId, out started))
    {
       // calculate time difference

    }
    return View("Index");
}
静态ConcurrentDictionary启动=新建ConcurrentDictionary();
公共操作结果启动(int workId)
{
starts.TryAdd(workId,DateTimeOffset.Now);
返回操作(“索引”);
}
公共操作结果停止(int workId)
{
DateTimeOffset start=DateTimeOffset.MinValue;
if(启动.TryGet(工作ID,输出启动))
{
//计算时差
}
返回视图(“索引”);
}

但这仍然不太好,因为在这段时间内,您的应用程序可能会被IIS重新启动,并且您将丢失starts值。当不再需要值时,它也没有清理表的代码。您可以通过使用.NET缓存来改进后者,但您确实需要一个数据库来做到这一点。

如果您希望为所有会话共享相同的实例(通常不希望),只需将其标记为静态

private static Stopwatch stopwatch = new Stopwatch();
此外,如果无法从其他控制器/程序集访问它,则不必将其定义为公共

正如@Ian Mercer所建议的那样,这不是一种用于秒表的好方法

请查看以下链接:


有趣的部分是
秒表的声明和实例化。请澄清这一点。假设
stopwatch
是控制器上的一个成员变量,它与
stopwatch
不同-控制器将为每个请求重新创建。您需要将秒表保留在某个位置。添加了秒表声明请记住web是无状态的。每个请求完全独立于其他请求。虽然有一些技巧可以模拟一种状态,比如
会话
变量、cookie等,但您必须仔细考虑在每种情况下使用何种方法,不要忘记可能会同时发生多个请求。这非常感谢-这似乎是一种更好的方法:)