I have a simple page with a button and an inscription on it. When I click the button, I want the label text to be updated, I copied the code from here
However, when I use the following example. The text is not updated until the function is completed. Any ideas. Windows WPF window With a button and a label fell on it.
I have code in code when the button is pressed and the first message is never displayed.
The Thread.Sleep - means pulling data from the database to return to the screen, it can take from 3 to 30 seconds, so I want to make it work to show something to the user.
Any ideas ???
using System.Threading;
using System.Windows;
using System.Windows.Threading;
namespace NetSpot.RESV4.Presentation
{
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);
}
}
}
source
share