A
A
Anton Baryshev2021-03-20 22:54:02
.NET
Anton Baryshev, 2021-03-20 22:54:02

How to refresh DataGrid data without freezing and over-loading?

Good evening everyone! I am developing a program to view complete information about the processes on the computer:
6056500ae65c6610304357.png

Automatic updating of the DataGrid is implemented in a separate thread by binding to the Processes property in the view model (I try to stick to MVVM) that implements the INotifyPropertyChanged interface:

public class MainVM : INotifyPropertyChanged
    {
        public MainVM()
        {
            // We perform the update operation in a separate thread
            Task.Run(() =>
            {
                // Endless cycle
                while (true)
                {
                    Processes = Process.GetProcesses().ToList();
                    Thread.Sleep(4000); // Updating every 4 seconds
                }
            });
        }
        private List<Process> _processes;
        public List<Process> Processes
        {
            get { return _processes; }
            set
            {
                _processes = value;
                OnPropertyChanged("Processes");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }


XAML markup for DataGrid:
<DataGrid Name="processesDataGrid" Grid.Row="1" AutoGenerateColumns="False" 
                  ItemsSource="{Binding Processes, UpdateSourceTrigger=PropertyChanged}">
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding MainModule.FileVersionInfo.CompanyName}" Value="Microsoft Corporation">
                            <Setter Property="Background" Value="HotPink"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.RowStyle>
            <DataGrid.Columns>
                <DataGridTextColumn Header="ID" Binding="{Binding Id}"/>
                <DataGridTextColumn Header="Name" Binding="{Binding ProcessName}"/>
                <DataGridTextColumn Header="Title" Binding="{Binding MainWindowTitle}"/>
                <DataGridTextColumn Header="Memory Size" Binding="{Binding PagedMemorySize64}"/>
                <DataGridTextColumn Header="Threads count" Binding="{Binding Threads.Count}"/>
                <DataGridTextColumn Header="Company" Binding="{Binding MainModule.FileVersionInfo.CompanyName}"/>
                <DataGridTextColumn Header="Description" Binding="{Binding MainModule.FileVersionInfo.FileDescription}"/>
                <DataGridTextColumn Header="Copyright" Binding="{Binding MainModule.FileVersionInfo.LegalCopyright}"/>
            </DataGrid.Columns>
        </DataGrid>


The list of processes is updated every 4 seconds, this is indicated in the view model:
while (true)
{
     Processes = Process.GetProcesses().ToList();
     Thread.Sleep(4000); // Updating every 4 seconds
}

But, the fact is that the application is very slow, and freezes every 4 seconds. It becomes difficult to use. Yes, I understand that this is all due to the load, but in the standard Windows task manager it still works without freezes. Therefore, I'm looking for a way to make updating the DataGrid as smooth as possible.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
edward_freedom, 2021-03-20
@AVollane

I do not think that all data is updated in the dispatcher at once, and not by event. Make instead List Observablecollection and change the state of a specific object and you will not need to update the entire list in 4 seconds

R
Roman, 2021-03-20
@prorom-exe

Using the ToList () method, you suck out all the objects, try manually using Select to take only those that are needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question