UpdateLayout on click WPF

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

  }
}
+2
source share
3 answers

, 4 , , , .

public partial class Window1 : Window
{
   readonly DispatcherTimer m_timer = new DispatcherTimer();
   public Window1()
   {
       InitializeComponent();

       m_timer.Interval = TimeSpan.FromSeconds(4);
       m_timer.Tick += TimerOnTick;
   }

   private void button1_Click(object sender, RoutedEventArgs e)
   {
       label1.Content = "Button 1 Clicked";
       m_timer.Start();
   }

   private void TimerOnTick(object sender, EventArgs eventArgs)
   {
       m_timer.Stop();
       label1.Content1 = ...
   }
+3

( ), , . . label1.UpdateLayout(), :

   private void button1_Click(object sender, RoutedEventArgs e)
   {
       label1.Content = "Button 1 Clicked";
       label1.UpdateLayout();
       ForceUIToUpdate();
       Thread.Sleep(4000);
       label1.Content = "button 1 Updated";
   }

.

. ForceUIToUpdate.

   public static void ForceUIToUpdate()
   {
       DispatcherFrame frame = new DispatcherFrame();
       Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Render, new     DispatcherOperationCallback(delegate(object parameter)
       {
           frame.Continue = false;
           return null;
       }), null);
       Dispatcher.PushFrame(frame);
   }

ForceUIToUpdate , Render, .

+2

, .

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        Task.Run(() =>
        {
            Dispatcher.Invoke(new Action(() =>
            {
                label1.Content = "Waiting";
            }), null);

            Thread.Sleep(2000);

            Dispatcher.Invoke(new Action(() =>
            {
                label1.Content = "Done";
            }), null);

        });
    }

, .

: async/wait

    private async void button1_Click(object sender, RoutedEventArgs e)
    {
        label1.Content = "Waiting";

        await Task.Run(() => Thread.Sleep(2000));

        label1.Content = "Done";
    }
0

All Articles