Set start time for Stopwatch program in C#

后端 未结 5 2226
粉色の甜心
粉色の甜心 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 09:48

    is the offset fixed or variable?

    In either case, have it as some member variable or const for the class and just add it.

    private TimeSpan offset = TimeSpan.FromSeconds(45);
    

    Then just change your tick to include it:

    var combined = ss.Elapsed + offset;
    int hrs = combined.Hours, mins = combined.Minutes, secs = combined.Seconds;
    
    0 讨论(0)
  • 2021-01-06 09:50

    You can't alter the start time, but you can modify it after the Stop() and no one is the wiser.

    A quick Google search: http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.start(v=vs.90).aspx

    A minor modification:

    class Program
    {
        static void Main(string[] args)
        {            
            System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
            
            stopWatch.Start();
            Thread.Sleep(10000);
            stopWatch.Stop();
            // Get the elapsed time as a TimeSpan value.
            TimeSpan ts = stopWatch.Elapsed;
            
            // Format and display the TimeSpan value.
            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                ts.Hours, ts.Minutes + 45 , ts.Seconds,
                ts.Milliseconds / 10);
            Console.WriteLine("RunTime " + elapsedTime);
        }
    }
    

    Result:

    RunTime 00:45:10.00

    Press any key to continue . . .

    A little more research on your part will help you immeasurably as a developer.

    0 讨论(0)
  • 2021-01-06 09:54

    Create a new instance of System.TimeSpan with the initial value of 45 minutes as per you example. Than add the value of the stopwatch to it TimeSpan.Add Method. Conveniently the Stopwatch.Elapsed is of type System.TimeSpan already.

    Than use string formatter to print the formatted time into the label. I think one of the other answers already shows how to do that. Otherwise here are some good references on how to format a TimeSpan instance TimeSpan.ToString Method (String) or using a custom formatter.

    var offsetTimeSpan = new System.TimeSpan(0,45,0).Add(ss.Elapsed)
    
    0 讨论(0)
  • 2021-01-06 09:55

    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;
                }
            }
        }
    
    0 讨论(0)
  • 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; }
        }
    }
    
    0 讨论(0)
提交回复
热议问题