Answer the question
In order to leave comments, you need to log in
How to organize constant updating of DataGrid in a separate thread?
Hello! I am developing an application for viewing processes on a computer and information about them.
But, immediately there was a need to constantly update the DataGrid in order to always receive up-to-date information about the processes. The first idea was to update the DataGrid in a separate thread every two seconds:
Task.Run(() =>
{
while (true)
{
// Обновление данных в DataGrid каждые две секунды
processesDataGrid.ItemsSource = Process.GetProcesses();
Thread.Sleep(2000);
}
});
Answer the question
In order to leave comments, you need to log in
How to organize a constant update of the DataGrid in a separate thread, so that the DataGrid is available, because it is planned to add the ability to select a row from the DataGrid and view detailed information.
using System;
using System.Threading.Tasks;
namespace Tasks
{
class Program
{
static async Task Main(string[] args)
{
// Так
await Task.Run(async () =>
{
while (true)
{
await Task.Delay(1000);
Console.Write("=");
}
});
// Или так (обрати внимание на Unwrap())
await Task.Factory.StartNew(async () =>
{
while (true)
{
await Task.Delay(1000);
Console.Write("=");
}
}).Unwrap();
}
}
}
There are two ways out:
1) make a datasource bind to a variable and update this variable, then there will be no problem with threads (you need to use MVVM)
2) change the datasource using the dispatcher
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question