10000个图像绑定到ListView时发生C#WPF contextswitch死锁 [英] C# WPF contextswitchdeadlock occured while 10000 images bind to listview
问题描述
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
<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]);
}));
将我的列表绑定到RunWorkCompleted中的listview
Bind my list to listview in RunWorkCompleted
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();
}
}
和XAML文件的外观<
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>
当我在listview中加载10000张图像时,一切正常,在3000到4000张图像上都可以正常工作
发生ContextSwitch死锁
我尝试过的事情:
我正在开发一个应用程序,我几乎已经完成了所有工作,对小数据没有问题,没有,每当发生错误时,我都会移至大文件,没有f文件.ContextSwitcherror
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
推荐答案
ContextSwitchDeadlock
是通常表明UI线程忙了太长时间.您是否由于误操作或故意禁用了VirtualStackPanel
?
这是一个包含100,000个项目的测试,它几乎可以立即加载并运行.重要的是,VirtualStackPanel
是活动的.
XAML
ContextSwitchDeadlock
is usually an indication that the UI thread was busy for too long. Have you disabled theVirtualStackPanel
by mistake or on purpose?
Here is a test with 100,000 items and it loads and runs almost instantly. Importantly, theVirtualStackPanel
is active.
XAML
<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
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 =
" , Age = rand.Next( 20 , 60 ) }); } } } 公共 类人 { 公共 字符串 AvatarUrl { get ; 设置; } 公共 字符串名称{ get ; 设置; } 公共 int 年龄{ get ; 设置; } }
"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; } }
这篇关于10000个图像绑定到ListView时发生C#WPF contextswitch死锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!