I'm having following code to show the current time:
static void Main(string[] args)
{
while (true)
{
ShowDate(GetTime(),GetDate());
//Here's my issue:
System.Threading.Thread.Sleep(1000);
}
}
//Clear Console and write current Date again.
static void ShowDate(String time, String date)
{
Console.Clear();
Console.WriteLine(date);
Console.WriteLine(time);
}
static string GetTime()
{
string timeNow = DateTime.Now.ToString("HH:mm:ss");
return timeNow;
}
static string GetDate()
{
string dateNow = DateTime.Now.ToString("dd.MM.yy");
return dateNow;
}
Well, in my understanding, Thread.Sleep(1000)
only shows the time every second measured after start of the program. So it doesn't show the perfectly correct time. I could try a lower value for Thread.Sleep()
, but that's still kinda inaccurate and probably becomes kind of inefficient.
Is there any possible way to, for example, update the time every time the system itself updates its time? Something like an event listener
maybe?