Show program runtime every 3 seconds

Viewed 153

I'm learning async in C# and want to show a program runtime every three seconds. I have two solutions, but neither works completely properly.

Solution 1

In the first solution, I have a loop where I call two methods. The first performs the calculation and the second shows the startup time, which is divisible by 3.

namespace Async
{
    class Program
    {
        static void Main(string[] args)
        {
            PerformLoop();

            Console.ReadLine();
        }

        public static async void PerformLoop()
        {
            Stopwatch timer = new Stopwatch();
            timer.Start();
            List<Task> l = new List<Task>();
            for (int i = 0; i < 50; i++)
            {
                l.Add(AsyncCalculation(i));
                l.Add(ShowTime(Convert.ToInt32(timer.Elapsed.TotalMilliseconds)));
            }
            await Task.WhenAll(l);
            timer.Stop();

            Console.WriteLine("Total execution time: " +
                timer.Elapsed.TotalMilliseconds);
        }

        public async static Task AsyncCalculation(int i)
        {
            var result = 10 * i;
            Console.WriteLine("Calculation result: " + result);
        }

        public async static Task ShowTime(int execTime)
        {
            if (execTime % 3 == 0)
            {
                Console.WriteLine("Execution time: " + execTime);
            }
        }
    }
}

Solution 2

In the second solution, I call two methods in a loop. The first performs calculations and the second displays the operation time after 3 seconds. Unfortunately, in this case, the second method blocks the execution of the first.

namespace Async
{
    class Program
    {
        static void Main(string[] args)
        {
            CallMethod();

            Console.ReadLine();
        }

        public static async void CallMethod()
        {
            for (int i = 0; i < 50; i++)
            {
                var results = Calculation(i);
                var calcResult = results.Item1;
                var time = results.Item2;

                ShowResult(calcResult);
                await ShowDelayTime(time);
            }
        }

        public static void ShowResult(int calcResult)
        {
            Console.WriteLine("Calculation result: " + calcResult);
        }

        public async static Task ShowDelayTime(int execTime)
        {
            await Task.Delay(3000);
            Console.WriteLine("Execution time: " + execTime);
        }

        public static Tuple<int, int> Calculation(int i)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            var result = 10 * i;

            stopwatch.Stop();

            return Tuple.Create(result,
                Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds));
        }
    }
}

I have no idea how to continuously display the calculation results and show the running time of the program by three seconds :(((

Edit

Expected output (example):

Calculation result: 0
Calculation result: 10
Execution time: 3 seconds
Calculation result: 20
Calculation result: 30
Calculation result: 40
Execution time: 6 seconds
Calcultion result: 50
//Next iterations

The program now shows the result, waits three seconds, and then go to next iteration. I want iterations for calculations to show regardless (independently) of time. I want the time to show every three seconds of the program running.

1 Answers

You could use a System.Threading.Timer in order to invoke a callback every 3 seconds, and a Stopwatch in order to measure the elapsed seconds since the start of the program:

var stopwatch = Stopwatch.StartNew();
var timer = new System.Threading.Timer(_ =>
{
    Console.WriteLine($"Execution time: {stopwatch.Elapsed.TotalSeconds:#,0} seconds");
}, null, 3000, 3000);
Related