WPF ProgressBar - TargetParameterCountException

时间:2010-06-18 03:13:44

标签: c# wpf progress-bar

1 个答案:

答案 0 :(得分:1)

上传程序(或任何异步组件)可以与创建它的线程同步。可能有多种方法可以做到这一点,但我之前见过的方法是这样的:

public class ResumableUploader {
  private SynchronizationContext _syncContext;
  public event EventHandler<ProgressChangedEventArgs> OnProgressChanged;

  public ResumableUploader() {
    _syncContext = SynchronizationContext.Current; //Think of this as the current thread
  }

  private ReportProgressChanged(int progress) {
     if(OnProgressChanged != null) {
         _syncContext.Send(s => { OnProgressChanged(this, new ProgressChangedEventArgs(progress)); }, null);  //s is any data you want to pass in, here it is unused
     }
  }
}

或者,如果用户/客户端在创建实例时提供了SynchronizationContext,则用户/客户端稍微更灵活但也更复杂:

public ResumableUploader(SynchronizationContext syncContext)