C#使用 线程 进行并发执行

这个博客展示了如何在C#中使用ParameterizedThreadStart和Thread类创建并控制多线程。代码创建了两个线程,thread1和thread2,分别用于更新Form1中的label1和label2文本。当按钮被点击时,线程开始运行,并通过Invoke方法安全地更新UI。线程的启动、挂起、恢复和终止操作也被讨论。
摘要由CSDN通过智能技术生成

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 bingfazhixingtest
{
  
    public partial class Form1 : Form
    {

        public ParameterizedThreadStart process1;
        public ParameterizedThreadStart process2;
        public Thread thread1;
        public Thread thread2;
        public bool bThread1;
        public bool bThread2;
        public Form1()
        {
            InitializeComponent();

//创建线程
            process1 = new ParameterizedThreadStart(Add1);
            process2 = new ParameterizedThreadStart(Add2);
            thread1 = new Thread(process1);
            thread2 = new Thread(process2);
      
        }

        private void Add1(object obj)
        {
            while (true)
            {
               
                    for (int a = 10; a < 100000; a = a + 2)
                    {
                        if (bThread1) {
                            this.Invoke(new Action(() => label1.Text = a.ToString()));
                            Thread.Sleep(200);
                        }
                            //label2.Text = a.ToString();
                        
                    }
                    Console.ReadLine();
               
            }
        }
        private void Add2(object obj)
        {
            while (true)
            {
              
                    for (int a = 10; a < 100000; a = a + 1)
                    {
                        if (bThread2)
                      {
                        this.Invoke(new Action(() => label2.Text = a.ToString()));
                        Thread.Sleep(200);
                        // label2.Text = a.ToString();
                        }
                    }
                    Console.ReadLine();

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //ThreadPool.QueueUserWorkItem(state => CountTo(int.Parse(textBox1.Text), cts.Token));
            bThread1 = true;        
            thread1.Start();
           
        }
        private void button2_Click(object sender, EventArgs e)
        {
            //创建两个变量,停止循环

            bThread2 = true;
            thread2.Start();

        }
      

        private void button3_Click(object sender, EventArgs e)
        {
            bThread1 = false;
           
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //bThread2 = false;
            thread2.Abort(); //线程销毁
        }
    }
}
 

线程的其他操作

thread2.Suspend();//挂起线程
thread2.Abort();//中止线程

thread2.Resume();//继续线程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值