My feeling is that you might not have many options other than running on the UI thread.
If it were a case that the long time involved was due to gathering and arranging the data then a background thread would be the best option, and then call any UI dependency properties using the Dispatcher object. I'm assuming the time is not to gather any data it is purely down to programmatic creation of the UIElements.
As I understand it UI elements derive from DispatcherObject
that gets the Dispatcher
from the current working thread so that rules out creation of controls on another thread.
Then all Dependency Properties call Dispatcher.VerifyAccess
when written to, that will raise an exception if accessed on the wrong thread. So that rules out updating databinding and properties on a different thread.
My first reaction would be like @Kent wrote.
To elaborate slightly, if you have a foreach
loop for each row with another foreach
for each column then you could, even though you are in the correct UI Thread, call Dispatcher.BeginInvoke
to "yield" momentarily from your long running method. i.e to break it up and then pass control back to the UI thread to later carry on adding to the grid. The layout effort might be significantly greater and the overall time would be longer but a bit more responsive.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
CreateRow(1);
}
private void CreateRow(int i)
{
//
// Construct row i of the grid
//
Dispatcher.BeginInvoke(DispatcherPriority.Background, new EventHandler(delegate { CreateRow(i + 1); }));
}