Silverlight Dispatcher 类

Dispatcher 类当前只提供从非用户界面 (UI) 线程在 UI 上运行代码的支持。

您可以通过 DependencyObject.Dispatcher 和 ScriptObject.Dispatcher 属性访问 UI 线程的 Dispatcher 对象。这些方法是实例方法,但这些类型的实例无法频繁从非 UI 线程访问。但是,该应用程序的 Deployment 对象是 DependencyObject,并且它可通过其 Current 属性用于任何线程。

您可以调用 CheckAccess 方法以确定调用方是否处于 UI 线程上。如果调用方不处于 UI 线程上,则可以调用 BeginInvoke 以便对 UI 线程运行指定的委托。

private delegate void AddTextDelegate(Panel p, String text);

private void AddText(Panel p, String text)
{
    p.Children.Clear();
    p.Children.Add(new TextBlock { Text = text });
}

private void TestBeginInvokeWithParameters(Panel p)
{
    if (p.Dispatcher.CheckAccess()) AddText(p, "Added directly.");
    else p.Dispatcher.BeginInvoke(
        new AddTextDelegate(AddText), p, "Added by Dispatcher.");
}
posted @ 2011-04-24 00:13  jeekun  阅读(1263)  评论(0编辑  收藏  举报