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

推荐订阅源

Google Online Security Blog
Google Online Security Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
C
Cybersecurity and Infrastructure Security Agency CISA
Cisco Talos Blog
Cisco Talos Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Scott Helme
Scott Helme
Project Zero
Project Zero
E
Exploit-DB.com RSS Feed
S
Secure Thoughts
K
Kaspersky official blog
L
Lohrmann on Cybersecurity
NISL@THU
NISL@THU
WordPress大学
WordPress大学
N
News and Events Feed by Topic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LINUX DO - 热门话题
小众软件
小众软件
P
Privacy & Cybersecurity Law Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
A
About on SuperTechFans
Hacker News: Ask HN
Hacker News: Ask HN
AWS News Blog
AWS News Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Hacker News: Front Page
F
Full Disclosure
Latest news
Latest news
Schneier on Security
Schneier on Security
The Hacker News
The Hacker News
T
Troy Hunt's Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Jina AI
Jina AI
Martin Fowler
Martin Fowler
P
Proofpoint News Feed
TaoSecurity Blog
TaoSecurity Blog
G
GRAHAM CLULEY
Forbes - Security
Forbes - Security
V
V2EX - 技术
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Vulnerabilities – Threatpost
C
Cyber Attacks, Cyber Crime and Cyber Security
MongoDB | Blog
MongoDB | Blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
P
Privacy International News Feed
C
Check Point Blog
N
News and Events Feed by Topic

博客园 - IamV

ADO.NET Entity Framework: 由 Entity Object 執行 SQL 指令 Expression<Func<int, bool>>与<Func<int, bool> search something from db How to Embed Silverlight Content in HTML [转]python文件操作/打开/删除文件/压缩文件 Flash与3D编程探秘 - 文章目录 silverlight RenderTransform 后台动画设置 2104 Couldn't download the silverlight application Cannot connect to external websites - WebClient - IamV .NET下多线程中设置按钮的文本 - IamV - 博客园 [Desklighter]在桌面上运行你的Silverlight文件 [转]Silverlight 2.0 正式版跨域提交数据 转:C#线程调用带参数的方法 [转]深入了解字符集和编码 Office SharePoint Server Search与Windows SharePoint Services Search 调用WCF返回Josn的两种方式 asp.net Excel_MyTest SharePoint-Server Farm Configuration Not Complete Using JSON with ASP.NET 3.5
Application.DoEvent()
IamV · 2008-11-23 · via 博客园 - IamV

原贴地址:Application.DoEvent()

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

在应用程序中使用多个线程可以将用户界面 (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}