I have a need to append some grid columns to a grid which initially only contains a few columns. Creating the columns is long-running, and I'm trying to use async/await, but am getting the "calling thread cannot access this object because a different thread owns it" exception, so can someone guide me as to the correct way to do this. The exception occurs on the AddRange call. Thanks in advance.
private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
List<GridViewColumn> cols = await NewGridColsAsync();
viewGridControl.Columns.AddRange(cols);
}
private async Task<List<GridViewColumn>> NewGridColsAsync()
{
List<GridViewColumn> cols = new List<GridViewColumn>();
await Task.Run(() =>
{
for (int i = 0; i < 100; i++)
{
cols.Add(new GridViewColumn());
}
});
return cols;
}