Answer the question
In order to leave comments, you need to log in
Why is the trigger not firing correctly?
Good day. I am writing a WPF application to work with a database, I ran into an unknown :) The MySQL server status must be updated periodically to display this status on the form: the red element is the offline server, the green element is the online server.
Item code:
<Grid Grid.Column="0" Grid.Row="2">
<Border>
<Border.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=MySQLServerStatus}" Value="False">
<Setter Property="Border.Background" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=MySQLServerStatus}" Value="True">
<Setter Property="Border.Background" Value="Green"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
</Grid>
public Boolean MySQLServerStatus { get; set; }
public MainWindowViewModel()
{
MySQLServerStatus = false;
TimerCallback timeCB = new TimerCallback(CheckServer);
Timer time = new Timer(timeCB, null, 0, 1000);
}
private async void CheckServer(object state)
{
bool status = false;
try
{
status = await Task<bool>.Run(() => Data.GetData.CheckServerStatus());
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
if (status != MySQLServerStatus)
{
MySQLServerStatus = status;
OnPropertyChanged("MySQLServerStatus");
}
}
}
public static async Task <bool> CheckServerStatus ()
{
bool serverStatus = await Task.Run(() =>
{
var connectionString = @"***********************************";
bool result = false;
using (MySqlConnection connect = new MySqlConnection())
{
try
{
connect.ConnectionString = connectionString;
connect.Open();
if (connect.State == ConnectionState.Open)
{
connect.Close();
result = true;
}
else
{
connect.Close();
result = false;
}
}
catch
{
result = false;
}
}
return result;
});
return serverStatus;
}
Answer the question
In order to leave comments, you need to log in
Alexander : try using
System.Windows.Threading.DispatcherTimer instead of System.Threading.Timer
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question