Delayed refresh

CSharp · Console

/Program.cs

using System;
using System.Threading.Tasks;

public class Program
{
	static DateTime _nextRefresh = DateTime.MaxValue;
	
	public static void Main()
	{
		MainAsync().GetAwaiter().GetResult();
	}
	
	public static async Task MainAsync()
	{
		for (int i=0; i<10; i++)
		{
			Update();
			await Task.Delay(20);
		}
		Console.WriteLine("Done updating!");
		await Task.Delay(3000);
		Console.WriteLine("Done!");
	}

	static private async void Update() 
    {
		Console.WriteLine("Update!");
        await RefreshInOneSecond();
    }

	static private async Task RefreshInOneSecond()
	{
		_nextRefresh = DateTime.Now.AddSeconds(1);
		await Task.Delay(1000);
		if (_nextRefresh < DateTime.Now)
		{
			_nextRefresh = DateTime.MaxValue;
			Refresh();
		}
	}
	
	static public void Refresh()
	{
		Console.WriteLine("Refresh!");
	}
	
}

;