WPF Refresh Label. Tired of not making it.

zequion 151 Reputation points
2020-11-07T07:27:05.79+00:00

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);

// REFRESH(). UPDATE LABEL. THIS ONLY SHOW "0" IN THE LABEL.
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();
                MyLabel.Refresh();
                System.Threading.Thread.Sleep(500);
            }
        }
    }

    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);
        }
    }
}
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,671 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. zequion 151 Reputation points
    2020-11-07T11:40:37.303+00:00

    Every time I have to update a label, do I have to create a Task? It does not serve me.

    Also, it is clear that the thread I am trying to do Refresh() in is not busy doing anything.

    Is this the mother of all Microsoft shit? It seems.

    1 person found this answer helpful.

  2. Emon Haque 3,176 Reputation points
    2020-11-07T09:46:42.447+00:00

    What I've read is nothing will be updated as long as the UI thread is busy. You've to do stuff or sleep/delay in another thread. Try updating your Label in a Task with App.Current.Dispatcher.Invoke(...). For example, have these in your MainWindow.xaml:

    <Window ...>
        <StackPanel>
            <Label x:Name="label"/>
            <Button Click="Button_Click" Content="Update"/>
            <TextBlock x:Name="block"/>
        </StackPanel>
    </Window>
    

    and in the Button_Click handler do these:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Task.Run(() =>
        {
            for (int i = 0; i < 10; i++)
            {
                App.Current.Dispatcher.Invoke(() =>
                {
                    label.Content = "Updating " + i;
                    block.Text = "going to sleep";
                });
                Task.Delay(500).Wait();
                App.Current.Dispatcher.Invoke(() =>
                {
                    label.Content = "Updated " + i;
                    block.Text = "awaken";
                });
                Task.Delay(1000).Wait();
            }
        });
    }
    
    0 comments No comments

  3. Paul Mc 1 Reputation point
    2022-07-01T16:11:15.483+00:00

    Yes, this is a steaming pile of crap.
    label.Text = "I want to see this text";

    Why would any developer not want the text to update at this point?
    The way Microsoft have designed this is utterly stupid.

    I would love to read an explanation from someone Microsoft as to WHY they did this.

    How to you explain to the person paying you to write software that you can't update the text on the screen?

    0 comments No comments