1

I have a simple code but this is not working how to fix this issue the thread is running but listview is not updating what is the problem can anyone just explain it?

public Form1()
    {
        InitializeComponent();
        listView1.View = View.Details;
        listView1.FullRowSelect = true;
        listView1.Columns.Add("Problems", 80);
        listView1.Columns.Add("Data", 120);
        listView1.Columns.Add("Registry Key", 130);
        listView1.Columns.Add("users", 80);
        Thread childThread = new Thread(getlist);
        childThread.Start();
    }
    public void getlist()
    {
        int i;
        for (i = 0; i < 40; i++)
        {
            add("a", "b", "c", "d");
        }

    }
    public void add(string prob, string reg, string data, string user)
    {
        String[] row = { prob, reg, data, user };
        ListViewItem item = new ListViewItem(row);
        ListViewItem v = listView1.Items.Add(item);

        item.Checked = true;

    }
crackdac0d3
  • 31
  • 1
  • 6

1 Answers1

4

There are few issues.

  1. You created a thread but not called to start.
  2. Even if you start the thread it won't work because you are not allowed to update controls from other thread than one created the controls (Often called UI Thread), code throws cross-thread error.

Fixing these two issues, your code should look like

.

public Form1()
{
    InitializeComponent();
    listView1.View = View.Details;
    listView1.FullRowSelect = true;
    listView1.Columns.Add("Problems", 80);
    listView1.Columns.Add("Data", 120);
    listView1.Columns.Add("Registry Key", 130);
    listView1.Columns.Add("users", 80);
    Thread childThread = new Thread(getlist);
    childThread.Start();
}
public void getlist()
{
    add("a", "b", "c", "d");
}
public void add(string prob, string reg, string data, string user)
{
    String[] row = { prob, reg, data, user };

    ListViewItem item = new ListViewItem(row);


    if (listView1.InvokeRequired)
    {
         listView1.Invoke(new MethodInvoker(delegate
         {
             listView1.Items.Add(item);
             item.Checked = true;

         }));
    }   
    else
    {
        listView1.Items.Add(item);
        item.Checked = true;
    } 


}
crackdac0d3
  • 31
  • 1
  • 6
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
  • Actually i am reading some registry but when i am starting a new thread the value comes vithin add(); but the listview is not updating y this happens i have used your code also but same problem and have use thread.start(); – crackdac0d3 Apr 23 '16 at 16:21
  • I guess, I got what is happening. Probably the control's handle does not yet exist, InvokeRequired searches up the control's parent chain until it finds a control or form that does have a window handle. If no appropriate handle can be found, the InvokeRequired method returns false. Check to see if `InvokeRequired` is returning `false` – Hari Prasad Apr 23 '16 at 16:33
  • have edited the code what i am doing is i am using a for loop there please see it and help me – crackdac0d3 Apr 23 '16 at 16:33
  • That's not an issue, try placing `Application.DoEvents()` after you started the thread. – Hari Prasad Apr 23 '16 at 16:36