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);
            }
        }
    }
}
4
  • Are you sure what Thread.Sleep(2000) does? Also what kind of progress you'd like to show? Commented Sep 11, 2014 at 9:58
  • Just wait for 2 seconds and see the population. I can see from the console but not from the listView. Commented Sep 11, 2014 at 9:59
  • 1
    Any call to Thread.Sleep raises a giant red flag for me Commented Sep 11, 2014 at 10:01
  • Same sort of question I saw on here today. Commented Sep 11, 2014 at 10:31

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for the answer, can you please show with some code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.