How to make a chronometer with millisecond accuracy?

Asked

Viewed 72 times

0

Hello, I’m a beginner and decided to create a stopwatch function to practice some techniques. The timer can mark the time downward (e.g.: 10.9.8.7.6.5) or upward (9.10.11.12), and the user can choose the start and end intervals. However, although my code is working perfectly to time seconds and, in my view, being quite clean and concise, the execution and processing time of the lines of code precludes any attempt to time thousandths with some accuracy. I would like to know if there is any way of optimizing it or if there is some function that I do not know and that could be applied in this case. From now on, thank you.

    class Timer
{
    private int _delay;

    public int Delay { set { _delay = value >= 0 ? value : 0; } }

    public void Counting(int initialValue, int finalValue)
    {
        int start = DateTime.Now.Second + _delay;
        int countingValue = initialValue;

        int i = 0;
        while (initialValue <= finalValue ? countingValue <= finalValue : countingValue >= finalValue)
        {
            start = start + i == 60 ? - i : start;

            if (DateTime.Now.Second == start + i)
            {

                Display(countingValue);

               countingValue = initialValue <= finalValue ? countingValue + 1 : countingValue - 1;

                i++;

            }
        }

    }

    public void Display(int countingValue)
    {
        Console.WriteLine(countingValue);
    }
}
  • First, you don’t need a class to do this. A method solves your problem with a small change. Only create classes if you need them. If you don’t have a reason to have one, don’t. Unless you want to do something else, but we can only assess what’s in your code and what you put in there that you want to do. Then, that’s the worst way to try it. It keeps the processor working like crazy for a despicable result. You have some answers here that help: https://answall.com/q/86014/101 and https://answall.com/q/211902/101.

  • Thank you very much for your reply, @Maniero . Reading the links you suggested, I learned a little about async methods and managed to implement "await Task.Delay();" in my code. The one thing I didn’t understand very well was this question of avoiding creating new classes. It would be possible to create a method alone (without being within a class), or you were referring to placing this method within the class where the Main is?

  • C# 9 is totally possible, before that it was possible to create a static class that is not a truth class. And of course it can be next to the Main(), but not necessarily.

1 answer

0

To not leave this question open if someone with the same problem finds this post, I will respond with what I learned from the posts suggested by @Maniero.

The way my old code worked required constant time-pass checking and this required not only processing power, but also slowed down verification (being noticeable especially when working with milliseconds). There are ways to achieve a better and less compromising result using asynchronicity and the method Task.Delay().

For this, it is necessary to add the modifier async in its method and using the await Task.Delay() to carry out the wait.

The corrected code for this formatting would be:

 public static async void Counting(int initialValue, int finalValue, int delayMiliseconds)
        {
            int countingValue = initialValue;
            delayMiliseconds = delayMiliseconds == 0 ? 1000 : delayMiliseconds;

            while (initialValue <= finalValue ? countingValue <= finalValue : countingValue >= finalValue)
            {
                Display(countingValue); //Função que fará o display da contagem

                await Task.Delay(delayMiliseconds);

                countingValue = initialValue <= finalValue ? countingValue + 1 : countingValue - 1;    
            }
        }

Read the following posts if you want to have a more detailed explanation about the subject:

How to make the app wait for a while?

How to use and what to use the timer function in C#?

Credits to Maniero.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.