Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Everyone.....

I have a list view , where i want to show a collection of data recursively ..., I use back ground worker to keep my user interface active .., i also use Parallel.ForEach to load the data in list view at a time .

My code is:
C#
public void bg_DoWork(object sender, DoWorkEventArgs e)
{

     Parallel.ForEach(this.listView1.Items.Cast<ListViewItem>(), list =>;
             {
                Function_ABCD(list);
             });
}


In my "Function_ABCD", i update my listview by adding huge number of listviewitem . my problem is that listview freezes when execution is going on.other function is running smoothly means i can minimize or maximize the form ,stop the execution etc....

how can i update the listview ?

Thanking You..
Posted
Updated 25-Feb-13 22:01pm
v2

1 solution

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:

C#
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
C#
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
 
Share this answer
 
Comments
krushna chandra jena 26-Feb-13 6:19am    
I have tried Your code, but it still does not work....listview freezes , but i have a question that the cross thread exception can be overcome by "Control.CheckForIllegalCrossThreadCalls=false " which given by windows form control ....is the problem comes only for cross thread exception ?
Jegan Thiyagesan 26-Feb-13 7:38am    
Hi,
I tested it with 1000 item, and i did not see the list view freezing, list view seems responsive.

you must remember the background worker is different thread to the main thread which created the listview. If you try to add items into list view in background worker, you will have cross thread exception.

you can switch off the cross thread check, but I wouldn't recommend.

Regards
Jegan
krushna chandra jena 26-Feb-13 8:18am    
Hi....
I want to say that listview responses but after complete the execution ,
when execution in going on then it hangs , i can not select any row of the listview.
Regards
Krushna
Jegan Thiyagesan 26-Feb-13 8:38am    
Can't you change the design, instead of grabbing the data from the list view. you separate the view from model, so that you have the collection of list view items "ListViewItemCollection", you do whatever you want with them in the parallel task and when the background worker replies you with progress, you update the listView with whatever available in the "ListViewItemCollection".

Jegan

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900