Answer the question
In order to leave comments, you need to log in
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:
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));
}
}
<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>
while (true)
{
Processes = Process.GetProcesses().ToList();
Thread.Sleep(4000); // Updating every 4 seconds
}
Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question