I want to make a grid element in wpf programmatically with c# code and because it needs a huge processing i used a thread for it.now when i want to assign it the visual studio says :
"The calling thread cannot access this object because a different thread owns it."
my code is something like this :
PrepareGrid gridMaker = new PrepareGrid();
Thread fetchGrid = new Thread(() => {
GridContainer.Dispacher.Invoke(() => GridContainer.Content = gridMaker.getGrid());
});
fetchGrid.SetApartmentState(ApartmentState.STA);
fetchGrid.Start();
PrepareGrid
is a class that prepares the grid and its getGrid
's method returns the result grid and GridContainer
is a WPF Scroll Viewer
i used nested dispatchers but it doesn't work
Why i can't use Task
for wpf elements? the process of making grid is so huge and Thread
uses only one core of processor as i know