Set start time for Stopwatch program in C#

后端 未结 5 2228
粉色の甜心
粉色の甜心 2021-01-06 09:29

I want to write a simple stopwatch program, I can make it work with the following code

    public Form1()
    {
        InitializeComponent();
    }
    Syst         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-06 10:00

    • additional SetOffset() method
    • additional initialization with offset;
    • overrides all of needed methods
    • have the same class name, so no need to change your project code

    .

    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; }
        }
    }
    

提交回复
热议问题