多线程之跨线程操作控件.pdf
《多线程之跨线程操作控件.pdf》由会员分享,可在线阅读,更多相关《多线程之跨线程操作控件.pdf(7页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、多线程之跨线程操作控件NET 原则上禁止跨线程访问控件,因为这样可能造成错误的发生,推荐的解决方法是采用代理,用代理方法来间接操作不是同一线程创建的控件。第二种方法是禁止编译器对跨线程访问作检查,可以实现访问,但是出不出错不敢保证Control.CheckForIllegalCrossThreadCalls=false;最近我在做一个项目,遇到了跨线程要去访问页面控件.但是总是提示出错,不能在其它线程中修改创建控件的线程的控件的值,后来采用了匿名代理,结果很轻松地解决了.解决过程如下:首先在窗体上,创建一个listbox 与 lable.using System;using System.Co
2、llections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Threading;namespace AccessControl public partial class Form1:Form public Form1()InitializeComponent();private void Form1_Load(object sender,EventArgs e)Threa
3、d newthread=new Thread(new ThreadStart(BackgroundProcess);newthread.Start();/定义一个代理/private delegate void CrossThreadOperationControl();private void BackgroundProcess()/将代理实例化为一个匿名代理CrossThreadOperationControl CrossDelete=delegate()int i=1;while(i5)/向列表框增加一个项目listBox1.Items.Add(Item +i.ToString();i+
4、;label1.Text=我在新线程里访问这个lable!;listBox1.Items.Add(label1.Text);listBox1.Invoke(CrossDelete);希望这个小技巧能够对你的学习和工作有所帮助.若有更好的办法来解决跨线程访问控件的问题,不防也拿出来大家分享一下.C#跨线程访问控件运行时错误,使用MethodInvoker 即可解决:原代码:private void btnOK_Click(object sender,EventArgs e)tslInfo.Text=请稍候.;Thread td=new Thread(new ThreadStart(run);td
5、.Start();/线程方法/private void run()this.tslInfo.Text=就绪;修改后:private void btnOK_Click(object sender,EventArgs e)tslInfo.Text=请稍候.;Thread td=new Thread(new ThreadStart(threadRun);td.Start();/原线程方法/private void run()文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D
6、7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1
7、ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D
8、7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1
9、ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D
10、7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1
11、ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D
12、7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7 this.tslInfo.Text=就绪;/线程方法/private void threadRun()MethodInvoker In=new MethodInvoker(run);this.BeginInvoke(In);我们在做winform 应用的时候,大部分情况下都会碰到使用多线程控制界面上控件信息的问题。然而我们并不能用传统方法来做这个问题,下面我将详细的介绍。首先来看传统方法:public partial class Form1:Form public Form1()InitializeComponent();priv
13、ate void Form1_Load(object sender,EventArgs e)Thread thread=new Thread(ThreadFuntion);thread.IsBackground=true;thread.Start();private void ThreadFuntion()while(true)this.textBox1.Text=DateTime.Now.ToString();Thread.Sleep(1000);运行这段代码,我们会看到系统抛出一个异常:Cross-thread operation not valid:Control textBox1 ac
14、cessed from a thread other than the thread it was created on.这是因为.net 2.0 以后加强了安全机制,不允许在winform 中直接跨线程访问控件的属性。那么怎么解决这个问题呢,下面提供几种方案。第一种方案,我们在Form1_Load()方法中加一句代码:private void Form1_Load(object sender,EventArgs e)Control.CheckForIllegalCrossThreadCalls=false;Thread thread=new Thread(ThreadFuntion);文档编
15、码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L
16、2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编
17、码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L
18、2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编
19、码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L
20、2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编
21、码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7thread.IsBackground=true;thread.Start();加入这句代码以后发现程序可以正常运行了。这句代码就是说在这个类中我们不检查跨线程的调用是否合法(如果没有加这句话运行也没有异常,那么说明系统以及默认的采用了不检查的方式)。然而,这种方法不可取。我们查看CheckForIllegalCrossThreadCalls 这个属性的定义,就会
22、发现它是一个static 的,也就是说无论我们在项目的什么地方修改了这个值,他就会在全局起作用。而且像这种跨线程访问是否存在异常,我们通常都会去检查。如果项目中其他人修改了这个属性,那么我们的方案就失败了,我们要采取另外的方案。下面来看第二种方案,就是使用delegate 和 invoke 来从其他线程中控制控件信息。网上有很多人写了这种控制方式,然而我看了很多这种帖子,表明上看来是没有什么问题的,但是实际上并没有解决这个问题,首先来看网络上的那种不完善的方式:public partial class Form1:Form private delegate void FlushClient()
23、;/代理public Form1()InitializeComponent();private void Form1_Load(object sender,EventArgs e)Thread thread=new Thread(CrossThreadFlush);thread.IsBackground=true;thread.Start();private void CrossThreadFlush()/将代理绑定到方法FlushClient fc=new FlushClient(ThreadFuntion);this.BeginInvoke(fc);/调用代理 private void T
24、hreadFuntion()while(true)this.textBox1.Text=DateTime.Now.ToString();Thread.Sleep(1000);使用这种方式我们可以看到跨线程访问的异常没有了。但是新问题出现了,界面没有响应了。为什么会出现这个问题,我们只是让新开的线程无限循环刷新,理论上应该不会文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8
25、HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q8C7文档编码:CW5D7U8H5O8 HR9L9L2J5H1 ZF9L2P4Q
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 多线程 线程 操作 控件
限制150内