Implement Application.DoEvents in WPF
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF" Height="200" Width="300">
<StackPanel>
<Button x:Name="btnWithout" Click="btnWithout_Click">Without DoEvents</Button>
<Button x:Name="btnWith" Click="btnWith_Click">With DoEvents</Button>
<ListBox x:Name="listBox"/>
</StackPanel>
</Window>
//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{}));
}
}
}
Related examples in the same category