As we say i am using a BackgroundWorker thread in a winforms and try to add item in DoWork method. So, for this situation we use code similar like this. it is working:
privatevoid backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { var test = new ListViewItem("item"); backgroundWorker1.ReportProgress(0, test); } privatevoid backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { listView1.Items.Add((ListViewItem)e.UserState); }
or
// avoid exception 'Cross thread exception' if we using this
ListViewItem _item = new ListViewItem();
if (listView1.InvokeRequired)
listView1.Invoke(new MethodInvoker(delegate
{
listView1.Items.Add(_item);
}));
else
listView1.Items.Add(_item);
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
or
// avoid exception 'Cross thread exception' if we using thisListViewItem _item = new ListViewItem();
if (listView1.InvokeRequired)
listView1.Invoke(new MethodInvoker(delegate
{
listView1.Items.Add(_item);
}));
else
listView1.Items.Add(_item);