0

I'm having issues with my script.

I'm trying to make a system whereby the system checks if there is a new row in MySQL and then displays it as a new item in a listbox using C# in WPF.

The issue is that it freezes the UI after a few seconds. I'm guessing it's because it's attached to the UI thread. I've tried using the basic timer but that doesn't even pull data from MySQL for some reason?

My code:

 public Dashboard()
        {
            InitializeComponent();
            LoadData();
            try
            {
                DispatcherTimer dispatcherTimer = new DispatcherTimer();
                dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
                dispatcherTimer.Interval = new TimeSpan(0, 0, 2);
                dispatcherTimer.Start();
            }catch { MessageBox.Show("Unable to reload data."); }

            }


        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            string si = Reports.SelectedItem.ToString();
            Reports.Items.Clear();
            LoadData();
            Reports.SelectedItem = si;

        }

        public void LoadData()
        {
            MySqlConnection con = new MySqlConnection(Func.Database.Con);
            con.Open();
            MySqlDataAdapter adapter;
            DataTable table = new DataTable();
            adapter = new MySqlDataAdapter("SELECT * FROM Events", con);
            adapter.Fill(table);
            
            foreach(DataRow dataRow in table.Rows)
            {
                Reports.Items.Add(("Event: " + dataRow["EventID"] + " \nType: " + dataRow["Header"] + "\nLocation: " + dataRow["Location"] + "\nGrade: " + dataRow["Grade"] +"\nStatus: " + dataRow["Status"] + "\n__________________"));
            }
            con.Close();

        }
Shadow
  • 33,525
  • 10
  • 51
  • 64
Nixfee
  • 21
  • 3

0 Answers0