Search code examples
c#wpfxamlbackgroundworker

How do I update a Label from within a BackgroundWorker thread?


When I used WinForms, I would have done this in my bg_DoWork method:

status.Invoke(new Action(() => { status.Content = e.ToString(); }));
status.Invoke(new Action(() => { status.Refresh(); }));

However in my WPF application I get an error saying that Invoke does not exist for Label.

Any help would be appreciated.


Solution

  • This will help you.

    To Execute synchronously:

    Application.Current.Dispatcher.Invoke(new Action(() => { status.Content = e.ToString(); }))
    

    To Execute Asynchronously:

    Application.Current.Dispatcher.BeginInvoke(new Action(() => { status.Content = e.ToString(); }))