views:

63

answers:

2

Hello All,

I realize this question could be boiled down to "Why is my code so slow?" but I'm hoping to get more out of that. Let me explain my code.

I have a class that implements INotifyPropertyChanged in order to do binding, and that class looks similar to this:

  public class Employee : INotifyPropertyChanged 
    { 
        string m_strName = "";
        string m_strPicturePath = "";
        public event PropertyChangedEventHandler PropertyChanged;

        public string Picture
        {
            get { return this.m_strPicturePath; }
            set { this.m_strPicturePath = value; 
            NotifyPropertyChanged("Picture"); }
        }

        public string Name
        {
            get { return this.m_strName; }
            set { this.m_strName = value;
            NotifyPropertyChanged("Name");
            }
        }

        private void NotifyPropertyChanged(String pPropName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(pPropName));
            }
        }
    }

In my XAML I've created a DataTemplate that binds to this object:

 <DataTemplate x:Key="EmployeeTemplate">
        <Border Height="45" CornerRadius="0" BorderBrush="Gray" BorderThickness="0" Background="Transparent" x:Name="bordItem">
            <Grid Width="Auto">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding Path=Name}" VerticalAlignment="Center" Padding="10" HorizontalAlignment="Stretch" FontWeight="Bold" FontSize="20"/>
                <Image Grid.Column="1" Source="{Binding Path=Picture}"></Image>
            </Grid>
        </Border>
    </DataTemplate>

and then put this template on a ListBox:

<ListBox x:Name="lstEmployees" ItemTemplate="{DynamicResource EmployeeTemplate}" VirtualizingStackPanel.VirtualizationMode="Recycling" VirtualizingStackPanel.IsVirtualizing="True"></ListBox>

So in code it's set as:

lstEmployees.ItemsSource = this.m_Employees;

the "m_Employees" list gets hydrated at app startup from a database, and then after that happens I set the above line of code. The ListBox is on a TabControl.

Now, my actual problem: My "m_Employees" list is returning about 500+ employees from the database, so the collection is slightly big. I get a performance hit in WPF only when the application first starts up and someone navigates to that tab with the ListBox on it. The UI freezes for about 3 seconds, but only when the app first starts up - afterwards it's fine.

Could this be because:

  • The code has to hit the hard drive to go find the image of each employee?
  • I am doing Virtualizing incorrectly?
  • EDIT
  • WPF is doing the rendering using my DataTemplate once, only when someone navigates to that TabControl, and is suddenly trying to draw 500+ employee items? If so, is there any way to "preload" the ListView in WPF?

Any other suggestions for improving the above would be apprecated. Thanks for reading and for any advice ahead of time.

-R.

A: 

The perf of your query is definitely suspect. If you want it to perform better, you can do any number of lazy initialization techniques to get it to run faster.

The easiest option would be to start with an empty enumeration, and only populate it at a later time.

The obvious way to do this would be to add a "Query" or "Refresh" button, and only freeze up the app when the user clicks it.

Another simple option is to queue a background task/thread to do the refresh.

If you are more concerned about consistent perf/super-responsive UI, then you should try to do more granular queries.

I am not sure if WPF handles virtualization of the items (only pulls from the enumeration when each item comes into view), but if it does, you could do paging/yield returns to feed ItemsSource.

If WPF just grabs the whole enumeration at once, you could still do smaller lazy-eval/paging, if you can determine which items are in view. Just populate the object with "zombie" items, and when they come into view, perform the query, and update the properties on the individual item.

Merlyn Morgan-Graham
Thanks for the suggestions. The "m_Employees" is a list of type Employee and that collection is fully populated on app startup [I wait for the collection to populate] - I may have described the problem badly. I suspect the performance hit is coming from WPF where when someone navigates to the tab with the listview, WPF is attempting to suddenly render 500+ items using my DataTemplate, and I think what I'm trying to solve is how to improve that speed. (Sorry for the confusing sentences, I've been up all night.)
Randster
@Randster - So you're saying, it's not hanging on the query - you've already done the query, the user clicks the tab, then the UI populates with the existing query results, and while populating, you get a ~3 second UI hang. Is this correct?
Merlyn Morgan-Graham
@Merlyn Morgan-Graham: yes, that is correct. If I set a breakpoint while debugging I can see that the collection object has populated with all 500+ employees. I'm wondering if there is a decent way to "preload" or thread the whole databinding rendering. I don't know enough about WPF to do so, so I'm looking for a least a link to a decent article or a pointer or to. Thanks :o)
Randster
@Randster: Another stab in the dark, perf-debugging-wise. You can try populating your UI with a static/empty image, and see if the perf picks up. If it does, then you can do the whole "zombie object" thing w/ the images, updating them one-by-one as they spool off the disk. Plenty of apps do that.
Merlyn Morgan-Graham
@Ranster: Unfortunately, I don't know any way to pre-load the rendering. The existing virtualization feature should take care of any issues w/ grabbing too many items at once (assuming it works - I've never played w/ it). I don't know of any articles for "threading the databinding", but I do know you could use an ObservableCollection, and populate it slowly, using a background thread to queue up list-adds on the main thread. That would allow you to feed items to it a few (50?) at a time, rather than all at once and hanging the app.
Merlyn Morgan-Graham
A: 
  1. Wrap m_Employees with a public property (Employees)
  2. Instead of setting your ItemsSource in the code like you do, set it with Binding and set IsAsync to True.

ItemsSource="{Binding Empolyess, IsAsync=True}"

You can also assign the Binding in the code.

Hope this helps.

Captain
Do you mind providing a link to an example? Google is not returning much help.
Randster
EDIT: Perhaps, like this?Binding _bindEmps = new Binding();_bindEmps.IsAsync = true;_bindEmps.Source = this.m_Employees;this.lstEmployees.SetBinding(ListBox.ItemsSourceProperty, _bindEmps);this.lstEmployees.ItemsSource = this.m_Employees;like so?
Randster
Binding _bindEmps = new Binding(); _bindEmps.IsAsync = true; _bindEmps.Source = this; _bindEmps.Path=Employees; this.lstEmployees.SetBinding(ListBox.ItemsSourceProperty, _bindEmps);
Captain
but its easier to do it in the XAML. Just wrap m_Employees with a public property: public List<Employee> Employees { get { return this.m_Employees; } set { this.m_Employees = value; }}. and in the xaml you set your ListBox's ItemsSource="{Binding Empolyess, IsAsync=True}". Just make sure to have the proper DataContext set.
Captain