rebel_UA
rebel_UA

Reputation: 6725

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?

Upvotes: 4

Views: 3100

Answers (1)

Justin Niessner
Justin Niessner

Reputation: 245479

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 };
);

Upvotes: 13

Related Questions