A
A
Alexander2016-08-11 05:49:18
WPF
Alexander, 2016-08-11 05:49:18

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>

In ViewModel:
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");
                }
            }
        }

Data.GetData.CheckServerStatus()
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;
            }

The DataTrigger normally fires only when the application starts: since the ViewModel class constructor defaults to false, we assume that the server is turned off all the time. If I run the application with MySQL server enabled - the border is as it should be green. If I turn off the server while the application is running, border.background is not updated, although the timer works correctly. Also, border.background works correctly if I change the MySQLServerStatus on a button on the form. I have two thoughts - either write a converter and not use a DataTrigger (may be the case in it), or the asynchronous delegate of the CheckServer(object state) timer cannot affect the variable in the main thread.
Please help :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
tex0, 2016-08-12
@saphire13

Alexander : try using
System.Windows.Threading.DispatcherTimer instead of System.Threading.Timer

#
#algooptimize #bottize, 2016-08-11
@user004

Google
Dependencyproperty
Inotifypropertychanged

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question