Refresh Label. Tired of not making it. RRS feed

  • Question

  • The WPF forum is not like this and they do not respond the same so I stop here again.

    It is impossible for me to do an Update Label from Wpf. It has never worked for me and I don't want to do something similar to a DoEvents () because I only want to update a label.

    // DOEVENTS(). This always works but lowers the performance of the computer and also I just want to update Label.
    System.Windows.Application.Current.Dispatcher.Invoke(new System.Action(() => { }), System.Windows.Threading.DispatcherPriority.ContextIdle, null);

     namespace Name_Wpf_Refresh_Test
         // -----------------------------------------
         // WPF. REFRESH.
         // -----------------------------------------
         public static class Cls_Wpf_Refresh_Test
         {   private static System.Action EmptyDelegate = delegate(){};
             public static void Refresh(this System.Windows.UIElement uiElement)
             {   uiElement.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, EmptyDelegate);
             public static void Fcn_Refresh_Test(System.Windows.Controls.Label MyLabel)
             {   for (int i = 0; i < 10; i++)
                 { MyLabel.Content = i.ToString();
         public class Cls_Main
             public static void Fcn_Refresh(System.Windows.Controls.Label MyLabel)
             { Name_Wpf_Refresh_Test.Cls_Wpf_Refresh_Test.Fcn_Refresh_Test(MyLabel);

    Saturday, November 7, 2020 8:52 AM

All replies

  • No, it cannot work the way you've written it. The reason is that Dispatcher.Invoke does not actually invoke the delegate that you pass. It just inserts it into the dispatch queue, so it will not execute until the dispatcher gets an opportunity to run through the queue. This will not happen while the main thread is still busy. And you keep it continually busy within your "for" loop. The Thread.Sleep does not free the thread; it just freezes the thread, so the dispatcher does not run at the time of the Sleep. If you want to free the thread so that the dispatcher runs, you need DoEvents instead of Sleep. If you do not want to use DoEvents, I suggest putting the whole "for" loop inside a separate thread and change the Text of the label within the delegate in the Dispatch.Invoke. The separate thread is necessary, otherwise the main thread will still be busy, so moving the Label assignment to the delegate will not fix anything.
    Saturday, November 7, 2020 10:35 AM
  • Hello,

    See if the following would work for you.

    Full source.

    Code behind

    using System;
    using System.Threading;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Threading;
    using ExceptionHandling;
    using WpfControlExtensions.Classes;
    namespace WpfControlExtensions
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
            public static RoutedCommand ClearLabelsRoutedCommand = new RoutedCommand();
            public MainWindow()
                CommandBindings.Add(new CommandBinding(
            private void ClearLabelsCanExecute(object sender, CanExecuteRoutedEventArgs e)
                e.CanExecute = true;
            private void ClearLabelsCommandOnExecute(object sender, ExecutedRoutedEventArgs e)
                var thread = new Thread(delegate()
                        Dispatcher.BeginInvoke(DispatcherPriority.Send, new Action(this.ResetLabels<Label>));
                    catch (Exception exception)
                }) {Name = "thread-clearing-demo"};

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    My GitHub code samples
    GitHub page

    Saturday, November 7, 2020 12:04 PM
  • Please don't post WPF questions in this forum. As has been mentioned before this is for C#-specific questions only. We're sorry if you're not getting a respond as fast as you like but it is forums so you may have to wait. WPF questions belong in the Microsoft Q&A forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, November 9, 2020 3:47 PM