zoukankan      html  css  js  c++  java
  • 线程操作,参考代码

    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)
            {
    
            }
          
        }
    }
  • 相关阅读:
    LayoutInflater作用及使用--自定义EditText,自带清除内容按钮
    SSL backend error when using OpenSSL pycurl install error
    pypyodbc 的坑
    mongo aggregate 删除重复数据
    如何验证代理ip的正确性
    python的非官方的一个下载lib的地方
    rabbitmq web 管理系统的信息
    内存泄漏分析
    readme 语法
    logging dictconfig
  • 原文地址:https://www.cnblogs.com/275147378abc/p/4618964.html
Copyright © 2011-2022 走看看