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 : 冰火战地
- c# 线程池 多线程
- c# 多线程(四) : 线程池
- C#多线程和线程池
- C#多线程和线程池
- c#多线程:线程池和异步编程
- c#多线程:线程池和异步编程
- C#多线程编程(3)-线程池ThreadPool
- c#多线程:线程池和异步编程
- C# 多线程的自动管理(线程池)
- C# 多线程的自动管理(线程池)
- C#多线程学习 之 线程池[ThreadPool]
- C# 多线程的自动管理(线程池)
- C#多线程学习 之 线程池[ThreadPool]
- C# 多线程的自动管理(线程池)
- C#多线程学习 之 线程池[ThreadPool]
- C#多线程学习 之 线程池[ThreadPool]
- C# 多线程的自动管理(线程池)
- C#多线程学习(四) 多线程的自动管理(线程池)
- Opencv学习笔记(九)光流法
- Uva 414 Machined Surfaces
- c程序员,必须记口诀.
- AsyncTask的用法
- Android 属性系统 Property service 设定分析
- c# 线程池 多线程
- mysql中的临时表用在什么地方比较好
- hdoj 1598 find the most comfortable road(并查集)
- MySQL 5.1 mysqldump导出备份(转)
- c# 多线程下载文件
- windows和centos的目录互访
- Qt键盘事件
- 工作问题。面试
- 学习的第十天