WPF不同线程之间的控件的访问
WPF不同线程之间的控件是不同访问的,为了能够访问其他线程之间的控件,需要用Dispatcher.Invoke执行一个新的活动即可。
例如:
public void SetNotes(string notes)
{
if (Dispatcher.Thread != Thread.CurrentThread)
{
this.txtNote.Dispatcher.Invoke(new Action(() =>
{
this.txtNote.Text += notes;
this.txtNote.Text += "\r";
this.txtNote.ScrollToEnd();
}));
}
else
{
this.txtNote.Text += notes;
this.txtNote.Text += "\r";
this.txtNote.ScrollToEnd();
}
}
WinForm中:
private delegate void delegateCrossThread(string message);
private void SetStatus(string message)
{
if (this.m_StatusLabel.InvokeRequired == true)
{
delegateCrossThread ct = new delegateCrossThread(SetStatus);
this.Invoke(ct, new object[] { message });
}
else
{
this.m_StatusLabel.Text = message;
this.m_StatusLabel.Refresh();
}
}
3、异步打开窗口
Thread newWindowThread = new Thread(new ThreadStart(ThreadStartingPoint));
newWindowThread.SetApartmentState(ApartmentState.STA);
newWindowThread.Start(); private void ThreadStartingPoint()
{
SurveyStatWindow surveyStatDialog = new SurveyStatWindow();
if (m_StatDataTable != null)
{
surveyStatDialog.TimeData = m_StatDataTable;
surveyStatDialog.Init();
}
surveyStatDialog.ShowDialog();
}
4、全局异步调用
Application.Current.Dispatcher.Invoke(new Action(() =>
{
AddText();
})); this.Dispatcher.Invoke(new Action(() =>
{
AddText();
})); Application.Current.Dispatcher.Invoke(new Action(delegate { AddText();}));
WPF不同线程之间的控件的访问的更多相关文章
- C#用副线程改主线程(UI线程)的控件属性的方法(包括Winform和WPF)
C#用副线程去试图修改主线程的UI控件会报出异常,解决方案是使用副线程注册事件通知主线程自己去修改UI控件 在winform中,方法如下 private void button1_Click(obje ...
- 扩展BindingList,防止增加、删除项时自动更新界面而不出现“跨线程操作界面控件 corss thread operation”异常
在做界面程序时,常常需要一些数据类,界面元素通过绑定等方式显示出数据,然而由于UI线程不是线程安全的,一般都需要通过Invoke等方式来调用界面控件.但对于数据绑定bindingList而言,没法响应 ...
- C#中线程对控件的访问
Control类提供了一个Invoke方法来给子线程访问主线程的控件,它的原型是酱紫的: object.Control.Invoke(Delegate method); object.Control. ...
- (转).NET 4.5中使用Task.Run和Parallel.For()实现的C# Winform多线程任务及跨线程更新UI控件综合实例
http://2sharings.com/2014/net-4-5-task-run-parallel-for-winform-cross-multiple-threads-update-ui-dem ...
- java笔记--使用事件分配线程更新Swing控件
使用事件分配线程更新Swing控件: Swing并不是线程安全的,如果在多个线程中更新Swing控件,则很可能造成程序崩溃. 为了避免这种问题,可以使用时间分配线程来更新Swing控件. EventQ ...
- VC线程中操作控件,引起程序卡死的问题。
[问题还原] 线程中操作控件,具体为控制一个按键的使能,使能后结束线程. 主程序中有一个死循环,等待线程结束. 然后,就没有然后了-- [解决方案] 在主程序死循环中,如果检测到界面消息,优先处理掉.
- C#子线程更新UI控件的方法总结
http://blog.csdn.net/jqncc/article/details/16342121 在winform C/S程序中经常会在子线程中更新控件的情况,桌面程序UI线程是主线程,当试图从 ...
- 在WinForm中使用委托来在其他线程中改变控件的显示
假设winform中有两个控件: 1.ListView用来显示进度的文本提示,ID:listView_progressInfo 2.ProgressBar用来显示进度,ID:progressBar1 ...
- 在C#中子线程如何操作主窗口线程上的控件
在C#中子线程怎样操作主线程中窗口上控件 在C#中,直接在子线程中对窗口上的控件操作是会出现异常,这是因为子线程和运行窗口的线程是不同的空间,因此想要在子线程来操作窗口上的控件.是不可能简单的通过控件 ...
随机推荐
- asp.net 對象圖解
- 使用innodb_ruby探查Innodb索引结构
使用innodb_ruby探查Innodb索引结构 innodb_ruby 是使用 Ruby 编写的 InnoDB 文件格式解析器.innodb_ruby 的目的是暴露一些其他隐藏的 InnoDB 原 ...
- .Net 面试题 汇总(四)
1.简述 private. protected. public. internal 修饰符的访问权限.private : 私有成员, 在类的内部才可以访问.protected : 保护成员,该类内部和 ...
- 远程计算机 进程/服务 启动停止(WMI)
WMI的远程管理需要其计算机的本地管理员组权限,例:gwmi win32_computersystem -computer win08r2d #在远程计算机上启动 notepad.exe 进程invo ...
- fiddler post 请求 webapi
今天小伙伴遇到一个问题,大概就是说用fiddler post 一个参数,但是后台一直无法获取,如下 后来发现请求的条件有问题,而且也很容易忽略,正确如下 content-type的设定为默认值,pos ...
- gradle结合spring-boot生成可运行jar包,并打印日志
1.用gradle把springboot项目打包成jar 1.1 build.gradle 中添加 buildscript { repositories { mavenLocal() maven { ...
- October 1st 2017 Week 40th Sunday
It's only after we've lost everything that we're free to do anything. 只有我们失去了所有之后我们才能随心而为. After los ...
- September 02nd 2017 Week 35th Saturday
Some things are more precious because they don't last long. 有些东西之所以弥足珍贵,是因为它们总是昙花一现. Life is ephemer ...
- 导出类成员里含有stl对象
How to export an instantiation of a Standard Template Library (STL) class and a class that contains ...
- ASP.NET Web Api vs Node.js Benchmark
http://mikaelkoskinen.net/post/asp-net-web-api-vs-node-js-benchmark ASP.NET Web Api vs Node.js Bench ...