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

推荐订阅源

宝玉的分享
宝玉的分享
NISL@THU
NISL@THU
E
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
K
Kaspersky official blog
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
S
Schneier on Security
G
GRAHAM CLULEY
The Hacker News
The Hacker News
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
爱范儿
爱范儿
P
Privacy International News Feed
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
S
Securelist
G
Google Developers Blog
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
美团技术团队
F
Fortinet All Blogs
小众软件
小众软件
Recorded Future
Recorded Future
V
Visual Studio Blog
B
Blog RSS Feed
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
Latest news
Latest news
Spread Privacy
Spread Privacy
H
Heimdal Security Blog

博客园 - 点点滴滴

C#操作IIS的代码 恢复误删数据(SQL Server 2000)--Log Explorer 如何让ClickOnce进行手动更新(含代码) - 点点滴滴 - 博客园 BackgroundWorker 组件 获取VS.NET 自带的数据库连接对话框的数据库连接 搜索一个局域网中所有的SQL Server服务器 在C#使用XML注释 用IDisposable接口释放.NET资源 正确的重载operator 很好的debug有理由不用吗 C#调用API访问其它进程 抽象 虚方法 接口 的区别 ASP.NET AJAX 路线图 ASP.NET AJAX 概述 安装ASP.NET AJAX Visitor Template Method Strategy State
Application.DoEvent()
点点滴滴 · 2006-12-16 · via 博客园 - 点点滴滴

丰富的用户体验是所有交互式应用程序的主要方面。需要用户交互的软件必须尽可能快地响应用户的活动。与此同时,应用程序必须能够处理数据以便将结果显示给用户。

在应用程序中使用多个线程可以将用户界面 (UI) 执行的任务与后台执行的任务分开。通过此方式组织任务,可以由 UI 响应用户输入,同时由后台进程进行数据处理。

创建辅助线程

应用程序可以创建一个或多个线程以执行方法。创建辅助(或从属)线程的第一个步骤是创建 ThreadStart 代理,指定要由该线程执行的方法。然后将 ThreadStart 代理传递给 Thread 类的构造函数。例如,要启动新的线程并执行 MyFunction 方法,请调用 Thread 类的 Start 方法,如下所示:

ThreadStart starter = new ThreadStart(MyFunction);
Thread t = new Thread(starter);
t.Start();

从辅助线程更新用户界面

您可以使用 Control.Invoke 从 UI 线程以外的其他线程更新用户界面 (UI)。此方法在 UI 线程上的控件线程上下文中执行代理。.NET Framework 精简版只支持 .NET Framework 完整版中的重载 Control.Invoke 方法。Control.Invoke 只使用一个参数:一个指定在 UI 线程上执行哪个方法的代理。该代理的类型必须为 EventHandler,并且具有以下签名:

void MyFunctionName(object sender, EventArgs e)

需要注意的一点是,如果要在辅助线程中更新 UI,就必须在代码中调用 Application.DoEvents()。调用 Application.DoEvents() 可确保辅助线程激发的任何事件都由该 UI 线程处理。

下面的示例代码说明如何创建辅助线程,然后从 UI 线程和辅助线程更新名为 listBox1 的 ListBox 控件:

 1

// 此变量将保留辅助线程设置的一些文本
 2public string Message = "";
 3
 4// 创建辅助线程,然后将项目从 UI 线程添加到
 5// ListBox 中
 6public void DoThreading()
 7{
 8    // 创建并启动辅助线程
 9    ThreadStart starter = new ThreadStart(this.UpdateListBox);
10    Thread t = new Thread(starter);
11    t.Start();
12
13    // 循环 4 次,每次都向 ListBox 中添加一条消息
14    for(int i = 0; i < 4; i++);
15    {
16        this.listBox1.Items.Add("来自 UI 线程的消息");
17        this.listBox1.Update();
18        // 处理 UI 线程中排队的事件
19        Application.DoEvents();
20        // 将进程挂起一秒钟
21        Thread.Sleep(1000);
22    }

23    this.listBox1.Items.Add("来自 UI 线程的上一条消息");
24    this.listBox1.Update();
25}

26
27public void UpdateListBox()
28{
29    for(int j = 0; j < 5; j++)
30    {
31        // 设置要从辅助线程添加到 ListBox 中的
32        // 消息
33        this.Message = "辅助线程的循环数 = " + j.ToString();
34        // 在 ListBox 的线程上下文中调用 WorkerUpdate 
35        // 方法
36        this.listBox1.Invoke(new EventHandler(WorkerUpdate));
37        Thread.Sleep(700);
38    }

39}

40// 为更新 ListBox 从辅助线程中
41// 调用的代理
42public void WorkerUpdate(object sender, EventArgs e)
43{
44    this.listBox1.Items.Add(this.Message);
45    this.listBox1.Update();
46}

47