When I used WinForms, I would have done this in my bg_DoWork
method:
status.Invoke(new Action(() => { status.Content = e.ToString(); }));
sta
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.
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.
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(); }))
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/
You need to use
Dispatcher.Invoke(new Action(() => { status.Content = e.ToString(); }))
instead of status.Invoke(...)