博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c#中如何跨线程调用windows窗体控件?
阅读量:4473 次
发布时间:2019-06-08

本文共 1835 字,大约阅读时间需要 6 分钟。

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()        {           // button1.Enabled = false;                    while (isRun)            {                             RunDele();           }            isRun = true;        }        void RunDele()        {            if (listView1.InvokeRequired)            {                RunDele dr = new RunDele(RunDele);                this.Invoke(dr);            }            else            {                listView1.Items.Insert(0, i.ToString());                i++;            }        }        void Exit()        {            listView1.Items.Insert(0, i.ToString());            i++;        }    }}

 

转载于:https://www.cnblogs.com/275147378abc/p/4615256.html

你可能感兴趣的文章
c3po数据库连接池中取出连接
查看>>
使用本机IP调试web项目
查看>>
【Java面试题】58 char型变量中能不能存贮一个中文汉字?为什么?
查看>>
C++ Primer 第六章 函数
查看>>
交互设计算法基础(3) - Quick Sort
查看>>
Ubuntu各种软件的安装
查看>>
java开发环境搭建-慕课网
查看>>
NOIP2015-D2T3运输计划
查看>>
Z :彻底了解指针数组,数组指针以及函数指针 [复
查看>>
2013年终总结
查看>>
Start to study Introduction to Algorithms
查看>>
AE常见接口之间的关系(较笼统)+arcgis常见概念
查看>>
正则表达式
查看>>
Mysql的DATE_FORMAT()日期格式转换
查看>>
SparkStreaming入门及例子
查看>>
Web应用增加struts2支持
查看>>
java程序——凯撒加密
查看>>
Windows Store App之数据存储
查看>>
English class 82 The Importance of traveling
查看>>
python用递归函数解汉诺塔游戏
查看>>