zoukankan      html  css  js  c++  java
  • Using Dispatcher

    he following code example shows how you might attempt to update the UI from your task logic.

    // The Wrong Way to Update a UI Object
    public void btnGetTime_Click(object sender, RoutedEventArgs e)
    {
       Task.Run(() => 
          {
             string currentTime = DateTime.Now.ToLongTimeString();
             SetTime(currentTime);
          }
    }
    private void SetTime(string time)
    {
       lblTime.Content = time;
    }

    If you were to run the preceding code, you would get an InvalidOperationException exception with the message ”The calling thread cannot access this object because a different thread owns it.” This is because the SetTime method is running on a background thread, but the lblTime label was created by the UI thread. To update the contents of the lblTime label, you must run the SetTime method on the UI thread.

    To do this, you can retrieve the Dispatcher object that is associated with the lblTime object and then call the Dispatcher.BeginInvoke method to invoke the SetTime method on the UI thread.

    The following code example shows how to use the Dispatcher.BeginInvoke method to update a control on the UI thread.

    // The Correct Way to Update a UI Object
    public void buttonGetTime_Click(object sender, RoutedEventArgs e)
    {
       Task.Run(() => 
          {
             string currentTime = DateTime.Now.ToLongTimeString();
             lblTime.Dispatcher.BeginInvoke(new Action(() => SetTime(currentTime)));
          }
    }
    private void SetTime(string time)
    {
       lblTime.Content = time;
    }

    Note that the BeginInvoke method will not accept an anonymous delegate. The previous example uses the Action delegate to invoke the SetTime method. However, you can use any delegate that matches the signature of the method you want to call.

  • 相关阅读:
    笔记:Maven 聚合和继承
    笔记:Maven 仓库和插件配置本机私服
    笔记:Maven 插件配置
    笔记:Maven 仓库及配置详解
    笔记:Maven 插件及配置详解
    笔记:Maven 依赖及配置详解
    笔记:Maven 项目基本配置
    【问题解决方案】Mathtype中丢失Mplugin.dll的问题
    【学习总结】Python-3-字符串函数-strip()方法
    【学习总结】Python-3-字符串函数split()的妙用
  • 原文地址:https://www.cnblogs.com/qixue/p/7365772.html
Copyright © 2011-2022 走看看