c# 线程池 多线程

来源:互联网 发布:索尼官方授权店淘宝 编辑:程序博客网 时间:2024/05/01 22:15


1。设置参数类

using System;
using System.Collections.Generic;
using System.Text;


public class stateinfo
{
    public int cookie;
    public stateinfo(int icookie)
    {
        cookie = icookie;
    }
}

-----------------------------------------------------------------------------------------------------------

线程开启方法类

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Threading;
using System.Windows.Forms;


public class operthread
{
    public Hashtable hashcount;
    public ManualResetEvent mrevent;
    public static int icount = 0;
    public static int imaxcount = 0;
    public static ListBox lis;
    public operthread(int maxcount)
    {
        hashcount = new Hashtable(maxcount);
        imaxcount = maxcount;
    }
    public void beta(Object state)
    {
        //这写线程处理事件
        lis.Items.Add(Thread.CurrentThread.GetHashCode().ToString() +"-"+ ((stateinfo)state).cookie.ToString());
        lis.Items.Add(hashcount.Count.ToString()+"-"+Thread.CurrentThread.GetHashCode().ToString());
        lis.Items.Add("-------------------------");
        lock (hashcount)
        {
            if (!hashcount.ContainsKey(Thread.CurrentThread.GetHashCode()))
            {
                hashcount.Add(Thread.CurrentThread.GetHashCode(), 0);
                hashcount[Thread.CurrentThread.GetHashCode()] = ((int)hashcount[Thread.CurrentThread.GetHashCode()]) + 1;
            }
        }
        Thread.Sleep(1000);
        Interlocked.Increment(ref icount);//原子操作递增变量存储
        if (icount == imaxcount)
        {
            lis.Items.Add("加载:"+icount.ToString());
            lis.Refresh();
            mrevent.Set();//将线程设置成终止
        }
    }
}

------------------------------------------------------------------------------------

队列如线程池:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections;
using System.Threading;
using System.Windows.Forms;

/// <summary>
/// simppool 的摘要说明
/// </summary>
public class simppool
{
public simppool()
{
  
}
    public static int main(ListBox lis)
    {
        bool flag = false;
        int maxcount = 10;
        ManualResetEvent mrevent = new ManualResetEvent(false);
        lis.Items.Add("Queuing"+ maxcount.ToString()+"items to Thread Pool");//线程总数
        lis.Refresh();
        operthread operthread1 = new operthread(maxcount);
        operthread1.mrevent = mrevent;
        try
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(operthread1.beta),new stateinfo(0));
            flag = true;//判断线程是否可队列
        }
        catch (NotSupportedException e1)
        {
            lis.Items.Add("出错了!操作系统版本太低需要win2000以上系统。");
            lis.Refresh();
        }
        if (flag)
        {
            for (int iitem = 1; iitem <= maxcount; iitem++)//循环开启线程进入队列
            {
                lis.Items.Add("插入队列:"+ iitem.ToString());
                lis.Refresh();
                ThreadPool.QueueUserWorkItem(new WaitCallback(operthread1.beta), new stateinfo(iitem));//将线程插入队列
            }
            lis.Items.Add("等待线程时间完成........");
            lis.Refresh();;
            mrevent.WaitOne(Timeout.Infinite, true);
            lis.Items.Add("加载线程.....");
            foreach (object o in operthread1.hashcount.Keys)//查看开启线程的总数
            {
                lis.Items.Add(o.ToString() + "-" + operthread1.hashcount[o].ToString());
                lis.Refresh();
            }
        }
        //Console.ReadLine();
        return 0;
    }
}

-----------------------------------------------------------------------------------------

调用

private void button3_Click(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;//允许线程访问控件
            operthread.lis = listBox1;
            simppool.main(listBox1);
        }



---by : 冰火战地