WPF C# Tutorial

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="200" Width="300">
    
        Without DoEvents
        With DoEvents
        
    


//File:Window.xaml.cs
using System.Windows;
using System.Windows.Threading;
namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        private delegate void EmptyDelegate();
        public Window1()
        {
            InitializeComponent();
        }
        private void btnWithout_Click(object sender, RoutedEventArgs e)
        {
            LoadNumbers(false);
        }
        private void btnWith_Click(object sender, RoutedEventArgs e)
        {
            LoadNumbers(true);
        }
        private void LoadNumbers(bool callDoEvents)
        {
            listBox.Items.Clear();
            btnWithout.IsEnabled = false;
            btnWith.IsEnabled = false;
            for(int i = 1; i <= 10000; i++)
            {
                listBox.Items.Add("Number " + i.ToString());
                if(callDoEvents){
                    DoEvents();
                }
                
            }
            btnWithout.IsEnabled = true;
            btnWith.IsEnabled = true;
        }
        public static void DoEvents()
        {
            Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background,new EmptyDelegate(delegate{}));
        }
    }
}