代码之家  ›  专栏  ›  技术社区  ›  Eric

在Silverlight中,如何在主调度线程上调用操作?

  •  5
  • Eric  · 技术社区  · 16 年前

    在WinForms UserControl中,我将通过从该控件的任何方法调用this.BeginInvoke()将数据传递给主GUI线程。Silverlight用户控件中的等效项是什么?

    换句话说,我如何获取由任意工作线程提供的数据并确保它在主dispatch线程上得到处理?

    2 回复  |  直到 16 年前
        1
  •  6
  •   Timothy Lee Russell    16 年前

    在UserControl类上使用Dispatcher属性。

    private void UpdateStatus()
    {
      this.Dispatcher.BeginInvoke( delegate { StatusLabel.Text = "Updated"; });
    }
    
        2
  •  2
  •   Ernest Poletaev    13 年前
        private void UpdateStatus()
        {
           // check if we not in main thread
           if(!this.Dispatcher.CheckAccess())
           {
              // call same method in main thread
              this.Dispatcher.BeginInvoke( UpdateStatus );
              return;
           }
    
           // in main thread now
           StatusLabel.Text = "Updated";
        }