Implement Application.DoEvents in WPF

image_pdfimage_print






//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{})); } } } [/csharp]