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

推荐订阅源

Help Net Security
Help Net Security
G
Google Developers Blog
雷峰网
雷峰网
WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
F
Full Disclosure
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
V
V2EX
C
Cisco Blogs
博客园 - 司徒正美
Project Zero
Project Zero
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
S
Securelist
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
T
Threatpost
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
S
Security Archives - TechRepublic
博客园 - Franky
N
News | PayPal Newsroom
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
W
WeLiveSecurity
A
Arctic Wolf
B
Blog

博客园 - ph580

国外大牛开发者创造出Siri第三方服务器 HTML5 开发者需要了解的技巧你要知道的知识 VS2008几大新功能描述如下 VS2008和.NET Framework 3.5功能与区别特性 竞争情报的战略与战术运用会让公司得利 为何中国企业在同一个问题上跌倒 开发嵌入式WEB的网络视频监控系统,设计思路 LightSwitch数据源开发小例 Amoeba新版本如何与MYSQL读写分离配置 SQL server 2005 master数据库进行轻型的恢复备份操作 安全、简单的Windows Forms多线程编程实现 如何下手进行高效的c#线程池设计 C#编程开发 Berkeley DB SQL使用方法 装箱和拆箱(什么是装箱和拆箱) C#基础 WCF中使用SoapHeader进行验证实现方法总结 JavaScript 正则表达式解析常用方法 C#正则表达式无法识别双引号解决 ASP.NET服务器控件开发之实现事件分析 Asp.net控件开发学习之数据回传小节
C#多线程窗体控件安全访问实现方法
ph580 · 2011-07-20 · via 博客园 - ph580

C# 2.0 为了线程安全,不充许子线程直接访问窗体中的控件
如果在子线程中直接访问说窗体控件,编译器会提示,控件不是
由该线程创建的.


那么在子线程中如何访问窗体中的控件呢?
在窗体的构造函数中加入这一句pdf
Control.CheckForIllegalCrossThreadCalls = false;
子线程就可以直接访问窗体中的控件了,不过这样线程是非安全的.
而默认Control.CheckForIllegalCrossThreadCalls=true;(捕获线程错误调用)
这时可以用Invoke

如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace Project2
{
   
public partial class Form1 : Form
    {
        
private BackgroundWorker backgroundWorker1;protected delegate void UpdateControlText(string strText);//定义一个委托
        
//定义更新控件的方法
        protected void updateControlText(string strText)
        {
           
this.label1.Text  = strText ;
            
return;
        }
public Form1()
        {
            
//Control.CheckForIllegalCrossThreadCalls = false;

            InitializeComponent();
        }
private void button1_Click(object sender, EventArgs e)
        {
            Thread ff 
= new Thread( new ThreadStart ( x2));
            ff.Start();
        }
private void x1()//线程安全的访问窗体控件
        {
            
for (int i = 0; i < 1000; i++)
            {
                
long xx = Convert.ToInt32(this.label1.Text);
                
if (this.InvokeRequired)
                {
                    UpdateControlText update 
= new UpdateControlText(updateControlText);//用更新控件的方法updateControlText实例化一个委托update
                    this.Invoke(update, Convert.ToString(++xx));//调用窗体Invoke方法

                }
                
else
                {
                    
this.label1.Text = Convert.ToString(++xx);
                }
            }
        }

     }

 }