Solve the flickering problem of C# listview multithreading call and notes

Lionsure 2020-08-13 Original by the website

Listview single-threaded(usually the thread that creates the listview control) does not cause flicker when calling the bound data, and the multi-threaded call of the listview is another world. From the beginning of adding data to the listview to the end of the binding, it keeps flickering. My eyes couldn't stand it after turning two pages.

How should this problem be solved? C# has provided a simple solution. Just add a sentence listview.BeginUpdate() before adding data to the listview, and add a sentence listview.EndUpdate() after adding data to the listview, and the C# listview flicker will be solved perfectly. In fact, the listview flicker is caused by refreshing the screen every time data is added due to change in the layout of listview. BeginUpdate() and EndUpdate() tell the listview not to refresh the screen after the start of adding data, just refresh once before the end of adding data.

 

1. Solve the flickering problem of C# listview multithreading call

The following is a complete example of a listview multithreading call flickering, the code is as follows:

using System.Threading;

/// <summary>
       /// Thread starts execution
       /// </summary>

       private void StartThread()
       {
              Thread thread = new Thread(new ThreadStart(ThreadListView));
              thread.Start();
       }

/// <summary>
       /// Solve the flickering problem of multithreading call of listview
       /// </summary>

       private void ThreadListView()
       {
              listView1.BeginUpdate();//Prevent listview flicker start
              AddListView();
              listView1.EndUpdate();//Prevent listview from flickering end
       }

/// <summary>
       /// Add data to the listview, call the control in multiple threads
       /// </summary>

       private void AddListView()
       {
              listView1.Clear();
              listView1.GridLines = true;
              listView1.View = View.Details;
              listView1.Scrollable = true;
              listView1.MultiSelect = false;

       listView1.Columns.Add("Product name", 180, HorizontalAlignment.Center);
              listView1.Columns.Add("Product price", 100, HorizontalAlignment.Center);

       ListViewItem lvi = new ListViewItem();
              lvi.SubItems.Clear();
              lvi.SubItems[0].Text = "Laptop";
              lvi.SubItems.Add("5000");
              listView1.Items.Add(lvi);

       lvi = new ListViewItem();
              lvi.SubItems[0].Text = "Brand-name mobile phone";
              lvi.SubItems.Add("2800");
              listView1.Items.Add(lvi);
       }

Call: StartThread();

 

 

2. C# listview multi-threaded call to prevent flicker[Notes]

If you add listView1.BeginUpdate(); before you start adding data to the listview, and the program returns on the way, you must add listView1.EndUpdate();, otherwise the listView has been in the unfinished state of bound data and affects the normal operation of the program. Such as:

listView1.BeginUpdate();
       if(Condition is true) return;//Error

if(Condition is true)//Correct
       {
              listView1.EndUpdate();
              return;
       }

ListViewItem lvi = new ListViewItem();
       lvi.SubItems.Clear();
       lvi.SubItems[0].Text = "Laptop";
       lvi.SubItems.Add("5000");
       listView1.Items.Add(lvi);

listView1.EndUpdate();