我们在用c# 开发程序时,经常会使用的多线程,实现多任务的处理。一般常用的方法是新建多个线程,进行处理。

   今天我分享一个采用线程池的方式来实现的实例。对有需要的朋友做个借鉴。

   实例: winform

  

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;
using system.threading;

namespace multithreading
{
    /// <summary>
    /// 此实例:为sdp软件快速开发平台中使用到的真实方法
    /// </summary>
    public partial class form1 : form
    {
        /// <summary>
        /// 私有:线程同步信号
        /// </summary>
        private manualresetevent cmdwaiter;

        /// <summary>
        /// 委托更新进度条
        /// </summary>
        private delegate void updatebar();

        /// <summary>
        /// 结束提示委托
        /// </summary>
        private delegate void showend();

        /// <summary>
        /// 任务队列
        /// 注意:此任务队列 需要用户自动来定义 
        /// 实例中采用 string 来处理
        /// </summary>
        private list<string> tasklist = new list<string>();

        /// <summary>
        /// 构造函数
        /// </summary>
        public form1()
        {
            initializecomponent();
        }

        /// <summary>
        /// 页面初始化加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void form1_load(object sender, eventargs e)
        {

            // 阻塞当前线程
            cmdwaiter = new manualresetevent(false);

            // 启动线程池
            threadpool.queueuserworkitem(new waitcallback(this.on_threadevent));

        }

        /// <summary>
        /// 线程处理事务
        /// </summary>
        /// <param name="obj"></param>
        private void on_threadevent(object obj)
        {
            while (true)
            {
                try
                {
                    // 阻塞当前线程,等待解除指令
                    this.cmdwaiter.waitone();

                    // 执行我们需要处理的事务
                    for (int k = 0; k < tasklist.count; k++)
                    {
                        run_mybusiness(tasklist[k]);

                        // 休息指定的毫秒
                        thread.sleep(50);
                    }


                    // 清除队列数据
                    this.tasklist.clear();

                    this.on_endlog();
                    this.cmdwaiter.reset();
                }
                catch (exception e)
                {
                    string strerror = e.message.tostring();
                    this.tasklist.clear();
                    this.cmdwaiter.reset();
                }
            }
        }

        /// <summary>
        /// 执行我们自己的业务
        /// </summary>
        /// <param name="str"></param>
        private void run_mybusiness(string str)
        {
            // 委托更新
            updatebar updatedelegate = new updatebar(on_update);
            this.invoke(updatedelegate);
        }

        /// <summary>
        /// 开始按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_start_click(object sender, eventargs e)
        {

            this.tasklist = new list<string>();
            for (int i = 1; i < 1001; i++)
            {
                this.tasklist.add(i.tostring());
            }


            this.txt_log.text = "开始:" + datetime.now.tostring("yyyy-mm-dd hh:mm:ss") + environment.newline;


            this.pg_bar.value = 0;
            this.pg_bar.maximum = this.tasklist.count;

            // 解除阻塞
            this.cmdwaiter.set();
        }

        /// <summary>
        /// 更新进度条
        /// </summary>
        private void on_update()
        {
            this.pg_bar.value = this.pg_bar.value + 1;
        }


        private void on_endlog()
        {
            showend enddeg = new showend(on_updatetextbox);
            this.invoke(enddeg);
        }

        private void on_updatetextbox()
        {
            string runstr = "结束:" + datetime.now.tostring("yyyy-mm-dd hh:mm:ss");
            this.txt_log.text = this.txt_log.text + runstr;
        }
    }
}

 

 运行效果:

 

下载地址:多线程实例 demo