UI Update Error

If I call to update a
private static UIElement Elements()
{
var panel = new Panel();

        txt3 = new Text(font, "SV")
        {
            ForeColor = Colors.White,
            HorizontalAlignment = HorizontalAlignment.Right,
        };

        txt3.SetMargin(50);
        panel.Children.Add(txt3);

}
If I call the below code from a button click, it works
private static void Button_Click(object sender, RoutedEventArgs e)
{

}
, but If I call from another class it give the below exception

    public static void TempSV_Update(String t)
    {
        Program.DB_Print("Temp SV Update");

        txt3_temp = t;

        txt3.TextContent = txt3_temp;
        txt3.Invalidate();
    }

Exception
image

Call stack
image

All UI elements are changed and updated from within the UI dispatcher:

User Interface (ghielectronics.com)

If want call from any function not in UI dispatcher thread, use below:

private void CallAnyWhere() => Application.Current.Dispatcher.Invoke(TimeSpan.FromMilliseconds(timeut), _ => {

           uiElement.Text = ..... // change

            return null;
        }, null);

Thanks, works good now :slight_smile:

1 Like