基本信息
源码名称:c#线程 线程循环
源码大小:0.10M
文件格式:.zip
开发语言:C#
更新时间:2020-07-06
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace 循环
{
   


    public partial class Form1 : Form
    {
        public delegate void listb(string msg1);
        public listb listb1;
        //public listb listb3;
        //public listb listb4;
        public delegate void tb(string msg2);
        public tb tb1;

        bool []m=new bool[100];

        int l = 0;
        string[] strArray1= new string[20];

        Thread t1, t2; // 说明为窗体类成

        public Form1()
        {
            InitializeComponent();
            listb1 = new listb(UpdateTxtMethod1);
            tb1 = new tb(UpdateTxtMethod2);
            //listb listb2 = new listb(UpdateTxtMethod1);
            //listb listb3 = new listb(UpdateTxtMethod2);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            m[0] = false;


            if (t2 != null && t2.ThreadState == ThreadState.Running)//判断线程是否处于运行状态 
            {

            }
            if (t2 != null && (t2.ThreadState & ThreadState.AbortRequested) != 0)//判断线程是否处于取消状态
            {

            }



            if (t2 == null  || (t2 != null && t2.IsAlive != true))
            {
                button1.Text = "线程启动中";
                t2 = new Thread(new ThreadStart(BackgroundProcess));
                t2.IsBackground = true;
                t2.Start(); 
            }

            else
            {
                m[0] = true;
                //t2.Abort();
                button1.Text = "线程已停止";
            }


            //if (button1.Text != "线程启动中")
            //{
            //    button1.Text = "线程启动中";
            //    t2 = new Thread(new ThreadStart(BackgroundProcess));
            //    t2.IsBackground = true;
            //    t2.Start(); // 
            //}
            //else
            //{

            //    t2.Abort();
            //    button1.Text = "线程已停止";
            //}







            //button1.Text = "线程启动";
            //Thread objThread = new Thread(new ThreadStart(delegate
            //{
            //    while (true)
            //    {
            //        Random random = new Random();
            //        int i = random.Next(10000);
            //        //Thread.Sleep(TimeSpan.FromSeconds(2));//暂停2S
            //        Thread.Sleep(100);
            //        this.BeginInvoke(listb1, i.ToString());

            //    }

            //}));
            //objThread.IsBackground = true;
            //objThread.Start();





        }

        public void UpdateTxtMethod1(string msg1)
        {
            if (l < strArray1.Length)
            {
                strArray1[l] = msg1;
                l ;
                listBox1.Items.Clear(); 
                for (int i = 0; i < l - 1; i )
                {
                    listBox1.Items.Add(strArray1[l - 1 - i]);
                }

            }
            else
            {
                Array.Copy(strArray1, 1, strArray1, 0, strArray1.Length - 1);
                strArray1[strArray1.Length - 1] = msg1;
                listBox1.Items.Clear();
                for (int i = 0; i < strArray1.Length; i )
                {
                    listBox1.Items.Add(strArray1[strArray1.Length - 1 - i]);
                }
            }


        }

        public void UpdateTxtMethod2(string msg2)
        {

            textBox1.AppendText(msg2 "\r\n");
            textBox1.ScrollToCaret();
        }



 

        private void button2_Click(object sender, EventArgs e)
        {
            Thread objThread = new Thread(new ThreadStart(delegate
            {
                int n = 10;
                this.BeginInvoke(tb1, "线程开始执行,执行" n "次,每一秒执行一次");
                for (int i = 0; i < n; i )
                {
                    this.BeginInvoke(tb1, i.ToString());
                    //一秒 执行一次
                    Thread.Sleep(1000);
                }
                this.BeginInvoke(tb1, "线程结束");
                
            }));
            objThread.IsBackground = true;
            objThread.Start();

        }
        Thread th1;
        //PictureBox[] picS = new PictureBox[8]; //
        //ListBox lis1 = new ListBox();
        TextBox tex1 = new TextBox();
        //public delegate void tex(TextBox tttt );
        //public tex tex1;
        //public delegate void tb(string msg2);
        //public tb tb1;

        private void button3_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;//设置该属性 为false


            //tex1 = textBox2;
            //tex1.Text = "ssssss" ;
            int content = 0;
            int.TryParse(textBox1.Text.ToString(), out content);  //表示将数字内容的字符串转为int类型,int.TryParse与 int.Parse类似,但它不会产生异常,转换成功返回 true,转换失败返回 false
            textBox1.Text = content.ToString();
            if (th1 == null)
            {
                th1 = new Thread(() =>
                {
                    int m=0;
                    
                    while (true)
                    {
                        
                        Thread.Sleep(10);
                        textBox2.BeginInvoke(new Action(() =>
                        {
                            
                            
                            textBox2.AppendText("ssssss" m.ToString() "\r\n");
                            textBox2.ScrollToCaret();
                            m ;

                        }));
                        if (m > 1000)
                        { 
                            break;
                        }
                    }
                })
                { IsBackground = true };
                th1.Start();
            }

        }

        private void button4_Click(object sender, EventArgs e)
        {
            Thread objThread = new Thread(() =>
            {
                int m = 0;
                Thread.Sleep(1);
                textBox3.BeginInvoke(new Action(() =>
                {
                    Thread.Sleep(100);
                    textBox3.AppendText("线程开始执行,执行" m "次,每一秒执行一次" "\r\n");
                    textBox3.ScrollToCaret();
                    while (true)
                    {

                        //textBox3.AppendText((10 - n).ToString() "\r\n");
                        //textBox3.ScrollToCaret();
                        //n--;
                        //if (n == 0)
                        //{
                        //    textBox3.AppendText("线程结束" "\r\n");
                        //    textBox3.ScrollToCaret();
                        //    break;
                        //}

                        
                        Thread.Sleep(100);
                        textBox3.BeginInvoke(new Action(() =>
                        {
                            textBox3.Text = "ssssss  " m.ToString();
                             textBox3.ScrollToCaret();
                        }));

                        m ;
                        if (m > 100)
                        {
                            break;
                        }

                    }



                }));



            });
            objThread.IsBackground = true;
            objThread.Start();

            //if (th1 == null)
            //{
            //    th1 = new Thread(() =>
            //    {
            //        int n = 10;
            //        Thread.Sleep(1);
            //        while (true)
            //        {
            //            m ;
            //            Thread.Sleep(1);
            //            textBox2.BeginInvoke(new Action(() =>
            //            {

            //                if (m > 100)
            //                {
            //                    m = 0;
            //                }
            //                textBox2.Text = "ssssss" m.ToString();

            //            }));
            //        }
            //    })
            //    { IsBackground = true };
            //    th1.Start();
           // }





        }

        //private void Form1_Load(object sender, EventArgs e)
        //{
            
        //    //t1 = new Thread(new ThreadStart(BackgroundProcess));
        //    //t1.Start();
        //}

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (t2!=null)
            {
                t2.Abort();
            }
           
           
        }
        int ii = 0;
        int iii = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();

            textBox4.Text = ii.ToString();
            if (ii<1000)
            {
                ii ;
            }
            else
            {
                ii = 0;
            }

            //for (int i = 0; i < 1000; i )
            //{
            //    //timer1.Interval = 100;
            //    textBox4.Text = i.ToString();
            //}
            //timer1.Interval = 10000;
            //for (int i =1000; i < 2000; i )
            //{

            //    textBox4.Text = i.ToString();
            //}
            //timer1.Stop();

            Thread.Sleep(1000);
           
            timer1.Interval = 1;
            timer1.Start();

            //timer1.Enabled = false;
            //try
            //{
            //    //执行获取数据和绑定数据源的代码

            //    timer1.Interval = 6000;
            //}
            //catch
            //{

            //    timer1.Interval = 30000;
            //}
            //finally
            //{
            //    timer1.Enabled = true;
            //}


        }

        private void button5_Click(object sender, EventArgs e)
        {
            timer1.Start();
            timer2.Start();
        }

        private void button6_Click(object sender, EventArgs e)
        {

        }

        private void button7_Click(object sender, EventArgs e)
        {

        }

        private void button8_Click(object sender, EventArgs e)
        {

        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            timer2.Stop();

            textBox5.Text = iii.ToString();
            if (iii < 1000)
            {
                iii ;
            }
            else
            {
                iii = 0;
            }

          
            Thread.Sleep(100);

            timer2.Interval = 1;
            timer2.Start();

        }

        private void BackgroundProcess()
        {
           
            while (true)
            {
                Random random = new Random();
                int i = random.Next(10000);
                //Thread.Sleep(TimeSpan.FromSeconds(2));//暂停2S
                Thread.Sleep(10);
                this.BeginInvoke(listb1, i.ToString());
                if (m[0]==true)
                {
                    m[0] = false;
                    break;
                }
            }
        }




    }