Start stopwatch from the specified time

I am trying to start a stopwatch from a given time (a decimal value output from a database). However, since Stopwatch.Elapsed.Add returns a new time interval rather than modifying the stopwatch, I cannot find a better way forward.

var offsetTimeStamp = new System.TimeSpan(0,0,0).Add(TimeSpan.FromSeconds((double)jd.ActualTime)); Stopwatch.Elapsed.Add(offsetTimeStamp); Stopwatch.Start(); 

Any ideas how I can do this? Greetings

+4
source share
4 answers

Normal StopWatch does not support initialization with offset timespan and TimeSpan is struct , therefore Elapsed is immutable. You can write a wrapper around StopWatch :

 public class StopWatchWithOffset { private Stopwatch _stopwatch = null; TimeSpan _offsetTimeSpan; public StopWatchWithOffset(TimeSpan offsetElapsedTimeSpan) { _offsetTimeSpan = offsetElapsedTimeSpan; _stopwatch = new Stopwatch(); } public void Start() { _stopwatch.Start(); } public void Stop() { _stopwatch.Stop(); } public TimeSpan ElapsedTimeSpan { get { return _stopwatch.Elapsed + _offsetTimeSpan; } set { _offsetTimeSpan = value; } } } 

Now you can add start-timespan:

 var offsetTimeStamp = TimeSpan.FromHours(1); var watch = new StopWatchWithOffset(offsetTimeStamp); watch.Start(); System.Threading.Thread.Sleep(300); Console.WriteLine(watch.ElapsedTimeSpan);// 01:00:00.2995983 
+4
source

The value of Elapsed read-only, which makes sense. The stopwatch simply measures the elapsed time between start and stop.

If you want to add a temporary value to a value, get the Elapsed value in the variable and add time to it after you measure it (i.e. after stopping).

0
source

I think you want to start your Stopwatch after a specific mount time specified by TimeSpan . I wonder why you do not want to start Stopwatch at the time specified by 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); 
0
source

If you place this file in your project, you have nothing to change in your project - this wrapper has the same name, all the same methods / parameters as the original stopwatch. But with the extra few:

  • optional method SetOffset ()
  • additional initialization with offset;

.

 using System; public class Stopwatch : System.Diagnostics.Stopwatch { TimeSpan _offset = new TimeSpan(); public Stopwatch() { } public Stopwatch(TimeSpan offset) { _offset = offset; } public void SetOffset(TimeSpan offsetElapsedTimeSpan) { _offset = offsetElapsedTimeSpan; } public TimeSpan Elapsed { get{ return base.Elapsed + _offset; } set{ _offset = value; } } public long ElapsedMilliseconds { get { return base.ElapsedMilliseconds + _offset.Milliseconds; } } public long ElapsedTicks { get { return base.ElapsedTicks + _offset.Ticks; } } } 
0
source

Source: https://habr.com/ru/post/1491606/


All Articles