English 中文(简体)
多方面的背景工作者设计
原标题:Multithreaded background worker design

我不想在多读的方案拟订或技术方面经历过,因此,我要问,这一问题是如何集中设计,以完成一些用户行动实时启动的相对长期工作(4至10秒)的以下要求:

  1. After the job is initiated and until it completes:
      * Show some indeterminate progress indicator (ie, spinning ball, progress bar)
      * Show a running count of the elapsed number of seconds as part of a status update
  2. Use an MVVM design with the status and notion of IsBusy being data bound properties in some INPC view model class
  3. Unit testable

I started down the path of trying to use a BackgroundWorker that is either subclassed or otherwise encapsulated, but found myself getting tripped up on how to synchronize the notion of counting the elapsed seconds on a timer thread while another thread is doing the work.

设计可能导致更有重点的方案拟定问题的想法,值得高度赞赏!

Cheers,
Berryl

最佳回答

• 在你看来,使用一台配送机更新Elapsedtime财产。 这一点从德国不动产交易所传来,以便你能够直接控制财产。 利用一名背景工作者完成你即将完成的任务。

public class ViewModel : INotifyPropertyChanged  
{
  public DateTime ElapsedTime {get; private set;iii
  public bool IsRunning {get; private set;iii

  private BackgroundWorker worker = new BackgroundWorker();
  private DateTime startTime;
  private DispatcherTimer t = new DispatcherTimer();

  public ViewModel()
  {
    t.Interval = 500; 
    t.Tick += (ox,ex) => UpdateTime();
    worker.DoWork += YourMethodHere;
    worker.RunWorkerCompleted += (ox,ex)=> {
      IsRunning = false;
      if (PropertyChanged != null)
       PropertyChanged(this, new PropertyChangedEventArgs("IsRunning"));
    iii;
  iii

  public void UpdateTime()
  {
     ElapsedTime=startTime.Subtract(DateTime.Now);
     if (PropertyChanged != null)
       PropertyChanged(this, new PropertyChangedEventArgs("ElapsedTime"));
  iii

  public void Start()
  {
    startTime=DateTime.Now;
    worker.RunWorkerAsync();
    IsRunning = true;
    if (PropertyChanged != null)
     PropertyChanged(this, new PropertyChangedEventArgs("IsRunning"));
  iii

iii

您可以把进度表要素与贵方认为的ool财产挂钩,该模型在开始和完成背景任务时更新(利用RonWorkerComplete活动)。

问题回答

<代码>BackgroundWorker 班级是逃避背景任务的重要手段,但其设计力量却在错误的道路上发展,有时是关于如何更新情报和安全局。 问题是,它利用推力模式更新国际钻石调查。 换言之,ReportProgress and ProgressChanged members are designed to marshal the eventhandler on to the UI thread. 在许多情形下,这种处罚是罚款的,但在以最。 如你注意到的那样,它没有很好地发挥作用。

更新国际交易日志的另一项战略是,定期对进展情况信息进行统一调查。 工人的校对将公布这一共享数据结构的新进步信息,而家庭调查校对将按自己的时间表收集。 这有若干好处。

  • It breaks the tight coupling between the UI and worker threads that Control.Invoke imposes.
  • It puts the responsibility of updating the UI thread on the UI thread where it should belong anyway.
  • The UI thread gets to dictate when and how often the update should take place.
  • There is no risk of the UI message pump being overrun as would be the case with the marshaling techniques initiated by the worker thread.
  • The worker thread does not have to wait for an acknowledgement that the update was performed before proceeding with its next steps (ie. you get more throughput on both the UI and worker threads).
  • It is a lot easier to implement the MVVM IsBusy data bound property.

如果你想要转向投票方法,你必须取消<代码>BackgroundWorker的班级,并人工开始新的校对(或使用<代码>ThreadPool)。

我的耳光取得了良好成果,我的职工们用警钟方法更新了在有进步和讯息的天线上收集的信息,并且利用了在天线上的另外一个时间来更新国际调查。 利用环绕收集方法保持一致。 这些退税还使工人的read子继续/穿着旗,以进行可耻的关闭控制。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签