Start a stopwatch from specified time

前端 未结 4 1780
一向
一向 2021-01-18 21:04

Im trying to start a stopwatch from a given time (decimal value pulled from a database). However, because the Stopwatch.Elapsed.Add returns a new timespan rather than modify

4条回答
  •  一个人的身影
    2021-01-18 21:28

    I think you want to start your Stopwatch after a certain mount of time specified by a TimeSpan. I wonder why you don't want to start your Stopwatch at a time specified by a DateTime instead?

    public class MyStopwatch : Stopwatch
    {
        public void Start(long afterMiliseconds)
        {
            Timer t = new Timer() { Interval = 1 };
            int i = 0;
            t.Tick += (s, e) =>
            {
                if (i++ == afterMiliseconds)
                {
                    Start();
                    t.Stop();
                }
            };
            t.Start();
        }
    }
    //use it
    var offsetTimeStamp = new System.TimeSpan(0,0,0).Add(TimeSpan.FromSeconds((double)jd.ActualTime));
    myStopwatch.Start((long)offsetTimeStamp.TotalMiliseconds);
    

提交回复
热议问题