Reputation: 303
I am writting a project on WPF. Before displaying main window, I must query some data from DB and then based on the data, I will draw some user control on main window by code behind. The processing take about 3 - 5 seconds. So when first launch mainwindows, It will display as white screen. After 3 - 5 seconds, the window will display fully.
I want to ask that, is there anyway to do display a waiting view and do the drawing user control in another thread. After finish, then load fully main window.
Please help me. Thanks in advance :).
Upvotes: 4
Views: 77
Reputation: 5135
You need to offload the UI Thread. You can do it by using either TPL
or BackgroundWorker
. I recommend you to use TPL
with async-await
, as it's much easier and the code is more clear.
private QueryResult QueryDatabase()
{
// Here's your db access code
return result;
}
private Task<QueryResult> QueryDatabaseAsync()
{
// This code will be queued to ThreadPool
return Task.Run(QueryDatabase);
}
private async void LoadedHandler(...)
{
IsProgressVisible = true;
Items = await QueryDatabaseAsync();
IsProgressVisible = false;
}
Upvotes: 2