Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:
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 25-Feb-13 22:49pm
Edited 25-Feb-13 23:01pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
krushna chandra jena at 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 at 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 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 190
1 Jochen Arndt 155
2 PIEBALDconsult 150
3 Afzaal Ahmad Zeeshan 120
4 DamithSL 115
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 26 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100