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

后端 未结 5 1919
慢半拍i
慢半拍i 2020-12-20 06:59

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

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


        
相关标签:
5条回答
  • 2020-12-20 07:15

    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.

    0 讨论(0)
  • 2020-12-20 07:18

    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.

    0 讨论(0)
  • 2020-12-20 07:32

    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(); }))
    
    0 讨论(0)
  • 2020-12-20 07:35

    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/

    0 讨论(0)
  • 2020-12-20 07:38

    You need to use

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

    instead of status.Invoke(...)

    0 讨论(0)
提交回复
热议问题