Load the Data for a Window Asynchronously After It Has Rendered

image_pdfimage_print



One Million Numbers:




//File:Window.xaml.cs
using System.Windows;
using System.Windows.Threading;
using System.Collections.Generic;

namespace WpfApplication1
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();

}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Dispatcher.BeginInvoke(DispatcherPriority.Background,new LoadNumbersDelegate(LoadNumbers));
}
private delegate void LoadNumbersDelegate();
private void LoadNumbers()
{
List numberDescriptions = new List();
for(int i = 1; i <= 1000000; i++) { numberDescriptions.Add("Number " + i.ToString()); } listBox.ItemsSource = numberDescriptions; } } } [/csharp]