C#多线程队列逐个执行

一个一个进度条逐个循环执行,

进度条带百分比(用一个Lable)放在progressBar上

用了跨线防问,多播委托,

源码下载地址链接: https://pan.baidu.com/s/1oAml5TS 密码: cc1j

下面是整个核心代码


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

namespace 作业队列
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            Thread thread = new Thread(() =>
            {
                zong(1);
            });
            thread.IsBackground = true;//当设为true 关闭程序  所有在进行的任务都结束 默认为false
            thread.Start();

        }
        private void zong(int i)
        {
            testc action = NewMethod1;
            action += NewMethod2;//多播委托
            action += NewMethod3;
            while (true)
            {
                action();
            }
           
        }
        private delegate void testc();
        private void NewMethod1()
        {
            try
            {
                for (int i = 0; i <= 100; i++)
                {
                    this.BeginInvoke((EventHandler)(delegate { progressBar1.Value = i; }));//异步调用线程用
                    System.Threading.Thread.Sleep(100);
                    double ii = i * 1.0 / 100 * 100;
                    this.BeginInvoke((EventHandler)(delegate { label1.Text = ii.ToString() + "%"; }));
                    if (i == 100)
                    {
                        this.BeginInvoke((EventHandler)(delegate { progressBar1.Value = 0; }));
                        this.BeginInvoke((EventHandler)(delegate { label1.Text = "0%"; }));
                    }
                }
            }
            catch { }
            
        }
        private void NewMethod2()
        {
            try {
                for (int i = 0; i <= 100; i++)
                {
                    this.BeginInvoke((EventHandler)(delegate { progressBar2.Value = i; }));
                    System.Threading.Thread.Sleep(100);
                    double ii = i * 1.0 / 100 * 100;
                    this.BeginInvoke((EventHandler)(delegate { label2.Text = ii.ToString() + "%"; }));
                }
            } catch { }

        }
        private void NewMethod3()
        {
            try
            {
                for (int i = 0; i <= 100; i++)
                {
                    this.BeginInvoke((EventHandler)(delegate { progressBar3.Value = i; }));
                    System.Threading.Thread.Sleep(100);
                    double ii = i * 1.0 / 100 * 100;
                    this.BeginInvoke((EventHandler)(delegate { label3.Text = ii.ToString() + "%"; }));
                    if (i == 100)
                    {
                        this.BeginInvoke((EventHandler)(delegate { progressBar3.Value = 0; }));
                        this.BeginInvoke((EventHandler)(delegate { label3.Text = "0%"; }));
                    }
                }
            }
            catch { }

        }
    }
}


评论