基本信息
源码名称:C# 线程池实现多线程 入门级实例
源码大小:0.04M
文件格式:.rar
开发语言:C#
更新时间:2016-03-22
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

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

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

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 ThreadMultiPro
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        #region 单进度条

        
        private void button1_Click(object sender, EventArgs e)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadStartMethod));
        }

        /// <summary>
        /// 用一个线程来启动
        /// </summary>
        /// <param name="arg"></param>
        public void ThreadStartMethod(object arg)
        {
            //初始化变量
            _index = 0; 
            _count = 0;
            _percent = 0;

            int workcount = 50;
            _count = 50 * 100;

            //实例化多线程辅助类并启动
            ThreadMulti thread = new ThreadMulti(workcount);

            thread.WorkMethod = new ThreadMulti.DelegateWork(DoWork);//执行任务的函数
            thread.CompleteEvent = new ThreadMulti.DelegateComplete(WorkComplete);//所有任务执行完毕的事件
            thread.Start();
        }

        #region 进度显示

        private int _index = 0;
        private int _count = 0;
        private int _percent = 0;
        private void IndexNext()
        {
            lock (this)
            {
                _index  ;
            }
        }

        private void DisplayProgress()
        {
            if (_percent != _index * 100 / _count)
            {
                _percent = _index * 100 / _count;

                this.BeginInvoke(new MethodInvoker(delegate()
                {
                    this.progressBar1.Value = _percent;
                }));

            }
        }
        #endregion

        public void DoWork(int index,int threadindex)
        {
            for (int i = 0; i < 100; i  )
            {
                Thread.Sleep(10);
                IndexNext();
            }
            DisplayProgress();
        }
        public void WorkComplete()
        {
            MessageBox.Show("ok");
        }
        #endregion

        #region 多进度


        private void button2_Click(object sender, EventArgs e)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadStartMethod2));
        }
        //启动
        public void ThreadStartMethod2(object arg)
        {
            int workcount = 50;
            _count = 50 * 100;

            ThreadMulti thread = new ThreadMulti(workcount);

            thread.WorkMethod = new ThreadMulti.DelegateWork(DoWork2);
            thread.CompleteEvent = new ThreadMulti.DelegateComplete(WorkComplete2);
            thread.Start();
        }
        //执行
        public void DoWork2(int index, int threadindex)
        {
            for (int i = 0; i < 100; i  )
            {
                Thread.Sleep(threadindex*5);
                DisplayProgress2(threadindex, index,i);
            }

        }
        //结束
        public void WorkComplete2()
        {
            MessageBox.Show("ok");
        }

        /// <summary>
        /// 进度显示
        /// </summary>
        /// <param name="threadindex"></param>
        /// <param name="taskindex"></param>
        /// <param name="progress"></param>
        private void DisplayProgress2(int threadindex,int taskindex,int progress)
        {
            switch (threadindex)
            {
                case 1:
                    this.BeginInvoke(new MethodInvoker(delegate()
                    {
                        label1.Text = taskindex.ToString();
                        this.progressBar2.Value = progress;
                    }));
                    break;
                case 2:
                    this.BeginInvoke(new MethodInvoker(delegate()
                    {
                        label2.Text = taskindex.ToString();
                        this.progressBar3.Value = progress;
                    }));
                    break;
                case 3:
                    this.BeginInvoke(new MethodInvoker(delegate()
                    {
                        label3.Text = taskindex.ToString();
                        this.progressBar4.Value = progress;
                    }));
                    break;
                case 4:
                    this.BeginInvoke(new MethodInvoker(delegate()
                    {
                        label4.Text = taskindex.ToString();
                        this.progressBar5.Value = progress;
                    }));
                    break;
                case 5:
                    this.BeginInvoke(new MethodInvoker(delegate()
                    {
                        label5.Text = taskindex.ToString();
                        this.progressBar6.Value = progress;
                    }));
                    break;
            }
        }
        #endregion
    }
}