I have a simple page with a button and a label on it. When I click the button I want the label text to update, I have copied code from here
However when i use the following example. The text is not updated until the function is completed. Any ideas. WPF windows form With button and label dropped on it.
I have the following in the codebehind the button depresses and the first message is never shown.
The Thread.Sleep is to signify pulling data from a database to return to the screen, this can take anything from 3 - 30 seconds, hence why I want to make it work to show the user something.
Any ideas ???
using System.Threading;
using System.Windows;
using System.Windows.Threading;
namespace NetSpot.RESV4.Presentation
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
label1.Content = "Button 1 Clicked";
ForceUIToUpdate();
Thread.Sleep(4000);
label1.Content = "button 1 Updated";
}
public static void ForceUIToUpdate()
{
DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Render, new DespatcherOperationCallback(delegate(object parameter)
{
frame.Continue = false;
return null;
}), null);
Dispatcher.PushFrame(frame);
}
}
}