分享

C#多线程队列逐个执行

 一点一唆 2018-04-14

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

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

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

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

下面是整个核心代码


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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 { }
 
        }
    }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多