线程操作,参考代码
2015-07-03 16:20
197 查看
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; //线程操作引用的命名空间 namespace windowform线程操作 { public delegate void RunDele();//定义一个委托,没有参数 public partial class Form1 : Form { Thread thread; //造一个线程对象 public Form1() { InitializeComponent(); // Control.CheckForIllegalCrossThreadCalls = false; //告诉程序不要检查线程的安全性,比较省力,但会对程序造成不好影响 } bool isRun = true; //点击启动 private void button1_Click(object sender, EventArgs e) { thread = new Thread(new ThreadStart(Run)); thread.IsBackground = true; thread.Start(); //委托开始执行 /*& button1.Enabled = false long i = 0; while (isRun) { listView1.Items.Insert(0,i.ToString()); i++; } isRun = true;*/ } private void button2_Click(object sender, EventArgs e) { isRun = false; button1.Enabled = true ; } long i = 0; void Run() { while (isRun) { RunDele(); } isRun = true; } void RunDele() { if (listView1.InvokeRequired)//获取一个值,该值指示调用方在对控件进行方法调用时是否必须调用 //Invoke方法,因为调用方位无创建控件所在的线程意外的线程中 { RunDele dr = new RunDele(RunDele); this.Invoke(dr); //this.BeginInvoke(dr); } else { listView1.Items.Insert(0, i.ToString()); i++; } } private void Form1_Load(object sender, EventArgs e) { } } }
相关文章推荐
- ThinkPHP3.2创建控制文件及调用控制器中的方法
- VKTM导致ASMB终止导致ORALCE实例宕
- RPC框架几行代码就够了
- Missing artifact javax.jms:jms:jar: Missing artifact com.sun.jdmk Missing artifact com.sun.jmx:jmxri
- php-cp介绍
- c++ primer 学习笔记 1
- 黑马程序员学习日记 Eclipse常用设置
- Weka开发[3]-Evaluation类
- C++写XML
- 谈话Java在ThreadLocal理解类
- web.xml中通过contextConfigLocation的读取spring的配置文件
- Windows下Python添加库(模块)路径
- Java常用正则表达式
- 用phpexcel插件导出excel2003
- eclipse下的jsp:The user operation is waiting for "Building workspace" to complete
- 时滞微分方程的Matlab解法之一dde23
- Matlab如何读入指定文件夹里的所有图片
- PHP函数补完:session_id()
- 循环队列-顺序存储-c语言实现
- Eclipse修改默认Author