Hello all,
I've been looking at various c# timing functions but can't seem to find the right one. The closest one I seem to have found is stopwatch function, that only measures the time it takes to complete a command. What i'm after is a stopwatch function that only measures the time it takes for a user to input a value.

for example:


            answer = num1 + num2;
            Console.WriteLine("Number 1: " + (num1));
            Console.WriteLine("Number 2: " + (num2));
            Console.WriteLine("Answer: " + (answer));
            Console.WriteLine("What is the Answer?");
            Console.WriteLine("1 - Add");
            Console.WriteLine("2 - Subtract");
            Console.WriteLine("3 - Multiply");
            Console.WriteLine("4 - Divide");
            input = Console.ReadLine();
            intInput = int.Parse(input);
            if (intInput == 1)
                Console.WriteLine("Correct! Well Done!");
                goto start;
            else if (intInput >= 2)
                Console.WriteLine("Sorry you are incorrect!");
            else if (intInput >= 5)
            Console.WriteLine("Please enter a number between 1 - 4 only!");
                goto op1r;

I want the stopwatch function to start as soon as the beginning of that code is called, and then i only want it to stop when the user has entered the correct number.

Any help to point me in the right direction would be greatly appreciated.

P.s Sorry if the code is messy.

I've found the solution! It was actually a lot easier than I thought! I just had to use a bit of common sense.

DateTime startTime = DateTime.Now; //Captures the current time.
DateTime stopTime = DateTime.Now; //Captures the time again.
TimeSpan duration = stopTime - startTime; // The second time recorded is subtracted from the first time. The final value is stored in local variable duration.
Console.WriteLine("seconds:" + duration.Seconds); // Displays the recorded time in seconds.


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.