Hi,
your code works. Please, check your methods IsInternetAvailable() and GetInternetTime().
My demo:
<Window x:Class="WpfApp1.Window037"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp037"
mc:Ignorable="d"
Title="Window037" Height="200" Width="400">
<Window.DataContext>
<local:VM/>
</Window.DataContext>
<Window.Resources>
<local:TextToColorConverter x:Key="TextToColorConverter"/>
</Window.Resources>
<StackPanel>
<Label Content="{Binding msg}" Foreground="{Binding msg, Converter={StaticResource TextToColorConverter}}"/>
</StackPanel>
</Window>
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
namespace WpfApp037
{
public class VM : INotifyPropertyChanged
{
private static System.Timers.Timer chkDate;
public string msg { get; set; }
public VM()
{
chkDate = new System.Timers.Timer();
chkDate.Interval = 500; // every five seconds
chkDate.Elapsed += VerifyDate;
chkDate.AutoReset = true;
chkDate.Enabled = true;
}
public void VerifyDate(Object source, System.Timers.ElapsedEventArgs e)
{
if (IsInternetAvailable())
{
if (DateTime.Today.ToString("dd-MMM-yyyy") == GetInternetTime().ToString("dd-MMM-yyyy"))
{
msg = "Machine date verified!";
}
else
msg = "Machine date is not correct!";
}
else
{
msg = "Machine date cannot be verified! Check internet connectivity !!!";
}
OnPropertyChanged(nameof(msg));
}
Random rnd = new();
private bool IsInternetAvailable() => rnd.NextDouble() > .5;
private DateTime GetInternetTime() => DateTime.Today.AddDays(rnd.Next(0, 2));
#pragma warning disable CS8612
public event PropertyChangedEventHandler PropertyChanged;
public event EventHandler? CanExecuteChanged;
public void OnPropertyChanged([CallerMemberName] String info = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(info));
}
public class TextToColorConverter : IValueConverter
{
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string str = value as string;
if (value == null || str == "Machine date verified!")
return new SolidColorBrush(Colors.LightGreen);
else
return new SolidColorBrush(Colors.Red);
}
#pragma warning disable CS8603
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
=> null;
}
}