Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Everything working fine on 3000 to 4000 images when i am loading 10000 images in listview every error occured
ContextSwitchDeadlock occured

i am reading all image source using this code
C#
<pre>  BitmapImage myBitmapImage = new BitmapImage();
                                        myBitmapImage.BeginInit();
                                        myBitmapImage.UriSource = new Uri(myImagesList[j].FilePath);
     myImagesList[j].getUri = myBitmapImage.UriSource;
     myBitmapImage.EndInit();
     this.Dispatcher.Invoke((Action)(() =>
        {
lstVisualDuplicateImage.Add(myImagesList[j]);
}));

Bind my list to listview in RunWorkCompleted
C#
private void FileLoadingWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
      {
          if(lvImages != null)
          {
              lvImages.ItemsSource = lstVisualDuplicateImage;
              //view = (CollectionView)CollectionViewSource.GetDefaultView(lvImages.ItemsSource);
              //PropertyGroupDescription groupDescription = new PropertyGroupDescription("GrpNumber");
              //view.GroupDescriptions.Add(groupDescription);
              //txtTotalDuplicates.Text = "Total Files" + "(" + lstVisualDuplicateImage.Count + ")";
              //txtTotalGroups.Text = "Total Groups" + "(" + GroupNo + ")";

              pop.HidePopUp();
          }
      }


and XAML file looks <
<Image Height="120" Width="120">

                                         <Image.Source>
                                             <BitmapImage
                     DecodePixelHeight="120"
                     DecodePixelWidth="120"
                     UriSource="{Binding Path=getUri, Mode=OneWay,UpdateSourceTrigger=Explicit }"
                     CreateOptions="IgnoreColorProfile"

                     CacheOption="None"  />
                                         </Image.Source>
                                     </Image>




Everything working fine on 3000 to 4000 images when i am loading 10000 images in listview every error occured
ContextSwitchDeadlock occured

What I have tried:

I am working on a application i have done almost everything no problem on small data no i move to large no f files every time error occured ContextSwitcherror
Posted
Updated 23-Sep-17 12:36pm

1 solution

ContextSwitchDeadlock is usually an indication that the UI thread was busy for too long. Have you disabled the VirtualStackPanel by mistake or on purpose?

Here is a test with 100,000 items and it loads and runs almost instantly. Importantly, the VirtualStackPanel is active.

XAML
XML
<Window
    x:Class="VirtualListView.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    mc:Ignorable="d"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    Title="Virtualized ListView" Height="300" Width="500">

    <Grid>
        <ListView ItemsSource="{Binding Persons}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Avatar" Width="100">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Image Source="{Binding AvatarUrl}" Margin="10" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Name" Width="200" DisplayMemberBinding="{Binding Name}" />
                    <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>

</Window>

Code Behind
C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        Mock();
    }

    public ObservableCollection<Person> Persons { get; set; }
        = new ObservableCollection<Person>();

    private Random rand = new Random();

    private void Mock()
    {
        for (int i = 0; i < 100000; i++)
        {
            Persons.Add(new Person
            {
                AvatarUrl = "http://www.freepngimg.com/download/happy_person/2-2-happy-person-free-download-png.png",
                Name = $"Person {i}",
                Age = rand.Next(20, 60)
            });
        }
    }
}

public class Person
{
    public string AvatarUrl { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}
 
Share this answer
 

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