Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add an item to a ListView from another thread without causing an exception

I try to add a row to a listView

listView1.Items.AddRange(new ListViewItem[] { item1 });

from a different thread to the one in which it was created and it throws an Exception.

Can anyone help me understand how to do this correctly?

like image 449
rebel_UA Avatar asked Dec 13 '22 20:12

rebel_UA


1 Answers

You can use Control.Invoke() to execute your code back on the UI thread:

listView1.Invoke(
    new MethodInvoker(delegate(){ 
        listView1.Items.AddRange(new ListViewItem[] { item1 };
);
like image 196
Justin Niessner Avatar answered Jan 18 '23 23:01

Justin Niessner