Hi,
You are trying to access a control in other threads that was created in main thread. The exception you will get from your code is the Cross-thread exception.
Try the code below:
delegate void SetListviewItemsCallback(ListView.ListViewItemCollection listViewItems);
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
ProcessListView(listView1.Items);
}
public void ProcessListView(ListView.ListViewItemCollection listViewItems)
{
if (listView1.InvokeRequired)
{
SetListviewItemsCallback d = new SetListviewItemsCallback(ProcessListView);
this.Invoke(d, new object[] { listViewItems });
}
else
{
ListView.ListViewItemCollection items = listViewItems;
Parallel.ForEach<ListViewItem>(items.Cast<ListViewItem>(), list =>
{
Function_ABCD(list);
});
}
}
This will get you out of cross thread exception when you are extracting the items from listView1, however, you have to do similar thing when you add the items into your
Function_ABCD(ListViewItem item)
. You will be accessing the "Function_ABCD" through multiple of thread, there will be cross thread exception as well as race condition; this will result in one item being added multiple of times which will cause an Argument exception.
I hope this helps.
Regards
Jegan