如何在使用BackgroundWorker时报告在类库中执行的工作进度

时间:2017-04-06 07:48:33

标签: c# wpf backgroundworker

如何报告BackgroundWorker在类库中的对象中执行的工作进度?

我到目前为止还不确定如何使用ReportProgress反馈信息(由于循环依赖性,无法引用调用类)。

这是启动工作的主要项目的一个例子:

namespace MainProject {
    class MainWindowVM : INotifyPropertyChanged {
        private BackgroundWorker _counterWorker;

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private int _progress;
        public int Progress {
            get { return _progress; }
            set { _progress = value; NotifyPropertyChanged(); }
        }

        public MainWindowVM() {
            var heavyWorker = new ClassLibrary.HeavyWorkerClass();
            _counterWorker = new BackgroundWorker();
            _counterWorker.DoWork += new DoWorkEventHandler(_counterWorker_DoWork);
            _counterWorker.RunWorkerAsync(heavyWorker);

        }

        private void _counterWorker_DoWork(object sender, DoWorkEventArgs e) {
            var heavyWorker = (ClassLibrary.HeavyWorkerClass)e.Argument;
            heavyWorker.StartWork();
            Progress = 100;
            System.Windows.MessageBox.Show("Work completed!");
        }
    }
}

以下是类库中上传方法的示例:

namespace ClassLibrary {
    public class HeavyWorkerClass {
        public void StartWork() {
            for (int i = 0; i <= 100; i++) {
                Thread.Sleep(50);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

HeavyWorkerClass必须以某种方式报告其进度。但是,你不应该对进展有任何了解。

例如,可以定期举起活动。然后,视图模型可以订阅此事件并相应地更新其Progress属性。视图绑定到视图模型的Progress属性。

请注意,服务或类库方法报告其进度的情况并不常见。这是因为在大多数情况下,根本没有API可以揭示正在进行的操作的当前进度。在这种情况下,您可以选择在UI中显示中间ProgressBar或类似内容,直到操作完成:

<ProgressBar IsIndeterminate="True" />

答案 1 :(得分:0)

按照mm8的解决方案,这是更新后的代码。

启动工作的主要项目:

namespace MainProject {
    class MainWindowVM : INotifyPropertyChanged {
        private BackgroundWorker _counterWorker;

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private ClassLibrary.HeavyWorkerClass _heavyWorker;
        public ClassLibrary.HeavyWorkerClass HeavyWorker {
            get { return _heavyWorker; }
        }

        public MainWindowVM() {
            _heavyWorker = new ClassLibrary.HeavyWorkerClass();
            _counterWorker = new BackgroundWorker();
            _counterWorker.DoWork += new DoWorkEventHandler(_counterWorker_DoWork);
            _counterWorker.RunWorkerAsync(_heavyWorker);
        }

        private void _counterWorker_DoWork(object sender, DoWorkEventArgs e) {
            var heavyWorker = (ClassLibrary.HeavyWorkerClass)e.Argument;
            heavyWorker.StartWork();
            System.Windows.MessageBox.Show("Work completed!");
        }
    }
}

执行耗时工作的HeavyWorkerClass:

namespace ClassLibrary {
    public class HeavyWorkerClass : INotifyPropertyChanged {

        private int _progress;
        public int Progress {
            get { return _progress; }
            set {
                _progress = value;
                NotifyPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public void StartWork() {
            for (int i = 0; i <= 100; i++) {
                Progress = i;
                Thread.Sleep(50);
            }
        }
    }
}