I am trying to load data asyncronously but on the first time, no progressbar is shown, its like the UI thread is busy calling the BackgroundWork and doesn’t render the controls. What I am doing wrong?
This is the async code, inspired by this question windows phone 7 progress bar for a listbox loading data
private void Embarques_Loaded(object sender, RoutedEventArgs e)
{
progressBar.Visibility = System.Windows.Visibility.Visible;
BackgroundWorker bw = new BackgroundWorker();
bw.RunWorkerCompleted += (s, ea) =>
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
progressBar.Visibility = System.Windows.Visibility.Collapsed;
txtUltimaAtualizacao.Text = DateTime.Now.ToString();
});
bw.DoWork += (s, ea) =>
Deployment.Current.Dispatcher.BeginInvoke(CarregaItens);
bw.RunWorkerAsync();
}
Assuming
CarregaItens()is your loading process, you shouldn’t callDeployment.Current.Dispatcher.BeginInvokefor it as that just elevates it from the background thread to the UI thread, thus freezing your app while it runs. Just call:bw.DoWork += (s, ea) => CarregaItens();