Set start time for Stopwatch program in C#

后端 未结 5 2272
粉色の甜心
粉色の甜心 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: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.

提交回复
热议问题