I want to write a simple stopwatch program, I can make it work with the following code
public Form1()
{
InitializeComponent();
}
Syst
Stopwatch does not have any methods or properties that would allow you to set a custom start time.
You can subclass Stopwatch and override ElapsedMilliseconds and ElapsedTicks to adjust for your start time offset.
public class MyStopwatch : Stopwatch
{
public TimeSpan StartOffset { get; private set; }
public MyStopwatch(TimeSpan startOffset)
{
StartOffset = startOffset;
}
public new long ElapsedMilliseconds
{
get
{
return base.ElapsedMilliseconds + (long)StartOffset.TotalMilliseconds;
}
}
public new long ElapsedTicks
{
get
{
return base.ElapsedTicks + StartOffset.Ticks;
}
}
}