在WinForms UserControl中,我可以通过从控件的任何方法调用this.BeginInvoke()来将数据传递给主GUI线程。在Silverlight UserControl中有什么等效的方法呢?
换句话说,我如何处理由任意工作线程提供的数据并确保它在主显示线程上得到处理?
换句话说,我如何处理由任意工作线程提供的数据并确保它在主显示线程上得到处理?
使用UserControl类的Dispatcher属性。
private void UpdateStatus()
{
this.Dispatcher.BeginInvoke( delegate { StatusLabel.Text = "Updated"; });
}
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";
}