GettingStarted
GettingStarted

Reputation: 7605

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.

Upvotes: 1

Views: 2712

Answers (5)

AndrewJE
AndrewJE

Reputation: 868

You really should think about using the power of "Data Binding" in WPF.

You should be updating an object in your view model and binding that to your user interface control.

See MVVM Light. Simple and easy to use. Don't code WPF without it.

Upvotes: 1

paul
paul

Reputation: 22001

If you're using WPF, I'd suggest looking into DataBinding.

The "WPF way" to approach this would be to bind the label's Content property to some property of your model. That way, updating the model automatically updates the label and you don't have to worry marshalling the threads yourself.

There are many articles on WPF and databinding, this is likely as good a place to start as any: http://www.wpf-tutorial.com/data-binding/hello-bound-world/

Upvotes: 2

Gopichandar
Gopichandar

Reputation: 2832

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

Upvotes: 1

Grant Winney
Grant Winney

Reputation: 66449

Use the capabilities already built into the BackgroundWorker. When you "report progress", it sends your data to the ProgressChanged event, which runs on the UI thread. No need to call Invoke().

private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
    bgWorker.ReportProgress(0, "Some message to display.");
}

private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    status.Content = e.UserState.ToString();
}

Make sure you set bgWorker.WorkerReportsProgress = true to enable reporting progress.

Upvotes: 8

Jens
Jens

Reputation: 2702

You need to use

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

instead of status.Invoke(...)

Upvotes: 1

Related Questions