惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
爱范儿
爱范儿
T
The Exploit Database - CXSecurity.com
量子位
T
Tailwind CSS Blog
T
Threatpost
The GitHub Blog
The GitHub Blog
AWS News Blog
AWS News Blog
云风的 BLOG
云风的 BLOG
K
Kaspersky official blog
P
Proofpoint News Feed
博客园 - 司徒正美
L
LangChain Blog
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
S
Secure Thoughts
The Last Watchdog
The Last Watchdog
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
T
Troy Hunt's Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
W
WeLiveSecurity
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
T
Tor Project blog
T
The Blog of Author Tim Ferriss
I
Intezer
P
Privacy & Cybersecurity Law Blog
美团技术团队
N
Netflix TechBlog - Medium
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
Google Developers Blog
Attack and Defense Labs
Attack and Defense Labs
T
Tenable Blog
月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
A
About on SuperTechFans
Last Week in AI
Last Week in AI

博客园 - pdfw

如何预编译ASP.Net程序 asp.net 输入框在chrome中无法关闭自动提示 sql server不能删除数据库,显示错误:正在使用 如何只更新datetime类型字段中的日期 SQL Server插入或修改数据是中文乱码的问题 怎么解决安装SqlServer2008总是提示Restart computer as failed 分页查询的SQL语句 如何使用Microsoft Enterprise Library里面的Log功能 【转】国外C#开源系统一览表 ,C# Open Source 【转】.NET试题总结二 【转】.NET试题总结一 窗口之间传递消息的一个方法 Dataset Designer在VS 2008里面不工作的解决办法 Flex中查找XML节点 - pdfw - 博客园 c#如何监视文件或者文件夹的变化 - pdfw - 博客园 如何在非英文环境中正确显示数字 SQL Server Express中连接字符串的问题 wpf制作毛玻璃效果按钮的代码 WPF中用于Path的Geometry Mini-Language
如何跨线程访问UI控件
pdfw · 2009-07-08 · via 博客园 - pdfw

当进行一些耗时的操作的时候,让UI所在的主线程进行处理,是不合适的,因为这样会使UI在操作的过程中停止响应。这时候需要使用多线程的方法进行处理。但是有一个问题就是一般情况下不能对UI控件进行跨线程的操作,下面是对这个问题的解决办法。

1、WinForm程序中

 a、第一种方法是使用BackgroundWorker控件,实例代码如下。

BackgroundWorker Demo 

b、 使用Thread/ThreadStart的跨线程操作控件,实例代码如下。

OperaControsInThreads Demo 

2、 WPF程序中, 使用Thread/ThreadStart的跨线程操作控件。

在WPF程序中, 控件没有InvokeRequired属性,这时候我们需要使用Dispatcher.CheckAccess()方法。 下面是在线程函数中调用的事件响应函数的例子。

        void OnDoWork(int, testInt, string testString)
        
{
            
if (!Dispatcher.CheckAccess())
            
{
                Dispatcher.Invoke(DispatcherPriority.Send, 
new OnDoWorkHandler(OnDoWork),  testInt, testString);
            }

            
else
            
{
                
//The code doing works. We can access the controls in UI.
            }

        }