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.
Using Dispatcher的更多相关文章
- 在非UI线程中自制Dispatcher
在C#中,Task.Run当然是一个很好的启动新并行任务的机制,但是因为使用这个方法时,每次新的任务都会在一个新的线程中(其实就是线程池中的线程)运行 这样会造成某些情形下现场调度的相对困难,即使我隔 ...
- Struts2 源码分析——调结者(Dispatcher)之执行action
章节简言 上一章笔者写关于Dispatcher类如何处理接受来的request请求.当然读者们也知道他并非正真的执行action操作.他只是在执行action操作之前的准备工作.那么谁才是正真的执行a ...
- Struts2 源码分析——调结者(Dispatcher)之action请求
章节简言 上一章笔者讲到关于struts2启动的时候加载对应的准备工作.如加载配置文件struts.xml之类的信息.而相应的这些操作都离不开Dispatcher类的帮助.如果读者只是认为Dispat ...
- 第一次部署Struts2时出现错误java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.class
报如下错误 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720) at org. ...
- WPF 线程 Dispatcher
WPF 应用程序从两个线程开始: 一个用于处理呈现 一个用于管理 UI 呈现线程有效地隐藏在后台运行,而UI线程则接收输入.处理事件.绘制屏幕以及运行应用程序代码. 大多数应用程序都使用一个 UI 线 ...
- System.Windows.Application.Current.Dispatcher.BeginInvoke
System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => ...
- Thinkphp源码分析系列(四)–Dispatcher类
下面我们来分析一下Thinkphp中的url解析和路由调度类.此类主要功能是 // +--------------------------------------------------------- ...
- 两种include方式及filter中的dispatcher解析
两种include方式 我自己写了一个original.jsp,另外有一个includedPage.jsp,我想在original.jsp中把includedPage.jsp引进来有两种方式: 1.& ...
- WPF入门教程系列四——Dispatcher介绍
一.Dispatcher介绍 微软在WPF引入了Dispatcher,那么这个Dispatcher的主要作用是什么呢? 不管是WinForm应用程序还是WPF应用程序,实际上都是一个进程,一个进程可以 ...
- Struts2 源码分析——调结者(Dispatcher)之准备工作
章节简言 上一章笔者讲到关于struts2过滤器(Filter)的知识.让我们了解到StrutsPrepareFilter和StrutsExecuteFilter的作用.特别是StrutsPrepar ...
随机推荐
- Linux 查看网卡流量、网络端口
查看网络流量 # 查看网卡流量 命令:sar -n DEV 1 10 注:每1秒 显示 1次 显示 10次 平均时间: IFACE rxpck/s txpck/s rxkB/s txkB/s rxcm ...
- 【题解】Luogu P4588 [TJOI2018]数学计算
原题传送门 这题是线段树的模板题 显而易见,直接模拟是不好模拟的(取模后就不好再除了) 我们按照时间来建一颗线段树 线段树初始值都为1,用来维护乘积 第一种操作就在当前时间所对应的节点上把乘数改成m ...
- 基于InfluxDB+Grafana打造大数据监控利器--转
这是一个大数据爆发的时代.面对信息的激流.多元化数据的涌现,我们在获取.存储.传输.理解.分析.应用.维护大数据时,无疑需要一种便捷的信息交流通道,以便快速.有效.准确地理解和驾驭这个过程.本文将通过 ...
- ES5的完美继承
// 定义一个动物类 function Animal (name) { // 属性 this.name = name || 'Animal'; // 实例方法 this.sleep = functio ...
- Spring中使用@Value读取porperties文件中的属性值方法总结及注意事项
本文为博主原创,转载请注明出处. 此前曾总结过使用工具类读取properties文件中的属性值,有兴趣的可以看一下. 如何快速获取properties中的配置属性值:https://www.cnblo ...
- Perl调用外部命令(其他脚本、系统命令)的方法和区别
1. `command`; 使用反引号调用外部命令能够捕获其标准输出,并按行返回且每行结束处附带一个回车.反引号中的变量在编译时会被内插为其值. 2. open LIST "ls -l| ...
- redis编译问题
在编译redis时,出现以下问题 In file included from adlist.c:34:0: zmalloc.h:50:31: fatal error: jemalloc/jemallo ...
- 软工实践练习一 关于GIT的使用
在Github上的操作部分: 1.在Github网站上进行注册.https://github.com/ 2.创建小组Organization. 3.将代码库https://github.com/sef ...
- 19 中山重现赛 1002 triangle
题意:给一组数据a[0]...a[n], n<5e6, a[i]<2^31-1(1e9)判断是否存在三角形数 首先想到的是排序,若a[i]+a[i+1]>a[i+2] , 则存在三 ...
- HashMap 和 HashTable差别
代码版本 JDK每一版本都在改进.本文讨论的HashMap和HashTable基于JDK 1.7.0_67.源码见这里 1. 时间 HashTable产生于JDK 1.1,而HashMap产生于JDK ...