0

I am loading from a database into a listView but I need to show the population progress while loading. Currently the data is displayed only after the loading is completed. This is what I have done so far, and thank you for any help:

string SQLCommand = "SELECT * FROM table";
using(SQLiteConnection con = new SQLiteConnection(connectionString))
{
    con.Open();
    using (SQLiteCommand cmd = new SQLiteCommand(SQLCommand, con))
    { 
        using (SQLiteDataReader rdr = cmd.ExecuteReader())
        {
            while (rdr.Read())
            {
                var v1 = (rdr[3].ToString());
                var v2 = (rdr[4].ToString());
                var v3 = (rdr[5].ToString());
                var v4 = (rdr[6].ToString());

                ListViewItem item1 = new ListViewItem(v1);
                item1.SubItems.Add(v4);
                item1.SubItems.Add(v2);
                item1.SubItems.Add(v3);
                lvwDetails.Items.AddRange(new ListViewItem[] { item1 });
                System.Threading.Thread.Sleep(2000);
            }
        }
    }
}
Doro
  • 671
  • 1
  • 16
  • 34

1 Answers1

2

You need to make the SQL processing happen in a background Thread or Task and the call the Dispatcher or UI thread to make the update.

Display the "Loading" info before starting the background task, then when the async task completes you should call the UI thread to update the UI with the data and also remove the loading info

I dont know all the context of you app but i assume that the code you show there is ran under the UI main thread, and putting Thread.Sleep isnt making it async, its just freezing your UI until the loading is done.

Alex Peta
  • 1,407
  • 1
  • 15
  • 26