- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Collections;
- using System.Threading;
- using System.Net.Sockets;
- namespace SpiderClient
- {
- class ThreadPool
- {
- //本客户度连接sockt
- private Client c = null;
- //线程池中数量
- private int threadcount;
- //保存所有线程池对象
- private ArrayList threads = null;
- //保存所有mywebrequest对象
- private ArrayList requests = null;
- //线程时候停止 可以外部控制
- public bool isstopthread = false;
- public ThreadPool(Client _c,int _threadcount)
- {
- //
- c = _c;
- threadcount = _threadcount;
- threads = new ArrayList();
- requests = new ArrayList();
- }
- public static ThreadPool CreateThreadPool(ThreadPool tp, Client _c, int _threadcount)
- {
- if (tp == null)
- {
- return new ThreadPool(_c, _threadcount);
- }
- else
- {
- tp.threads.Clear();
- tp.requests.Clear();
- return tp;
- }
- }
- public void StartPool()
- {
- Thread t = null;
- MyWebRequest mwr = null;
- for (int i = 0; i < threadcount; i++)
- {
- t = new Thread(new ParameterizedThreadStart(ThreadFunc));
- requests.Add(mwr);
- threads.Add(t);
- t.Start(i);
- }
- }
- public void StopAllThreads()
- {
- isstopthread = true;
- for (int i = 0; i < threadcount; i++)
- {
- Thread t = (Thread)threads[i];
- if (t != null)
- {
- if (t.ThreadState == ThreadState.Suspended)
- {
- t.Start();
- }
- t.Abort();
- }
- }
- }
- private void ThreadFunc(object o)
- {
- int i = (int)o;
- while (!isstopthread)
- {
- Thread.Sleep(100);
- }
- }
- }
- }
=========================================================================
简单代码如下:
- using System;
- using System.Threading;
- public class Example {
- public static void Main() {
- // Queue the task.
- ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
- Console.WriteLine("Main thread does some work, then sleeps.");
- // If you comment out the Sleep, the main thread exits before
- // the thread pool task runs. The thread pool uses background
- // threads, which do not keep the application running. (This
- // is a simple example of a race condition.)
- Thread.Sleep(1000);
- Console.WriteLine("Main thread exits.");
- }
- // This thread procedure performs the task.
- static void ThreadProc(Object stateInfo) {
- // No state object was passed to QueueUserWorkItem, so
- // stateInfo is null.
- Console.WriteLine("Hello from the thread pool.");
- }
- }
另外一个例子是关于自定义数据参与线程池处理的代码:
- using System;
- using System.Threading;
- // TaskInfo holds state information for a task that will be
- // executed by a ThreadPool thread.
- public class TaskInfo {
- // State information for the task. These members
- // can be implemented as read-only properties, read/write
- // properties with validation, and so on, as required.
- public string Boilerplate;
- public int Value;
- // Public constructor provides an easy way to supply all
- // the information needed for the task.
- public TaskInfo(string text, int number) {
- Boilerplate = text;
- Value = number;
- }
- }
- public class Example {
- public static void Main() {
- // Create an object containing the information needed
- // for the task.
- TaskInfo ti = new TaskInfo("This report displays the number {0}.", 42);
- // Queue the task and data.
- if (ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc), ti)) {
- Console.WriteLine("Main thread does some work, then sleeps.");
- // If you comment out the Sleep, the main thread exits before
- // the ThreadPool task has a chance to run. ThreadPool uses
- // background threads, which do not keep the application
- // running. (This is a simple example of a race condition.)
- Thread.Sleep(1000);
- Console.WriteLine("Main thread exits.");
- }
- else {
- Console.WriteLine("Unable to queue ThreadPool request.");
- }
- }
- // The thread procedure performs the independent task, in this case
- // formatting and printing a very simple report.
- //
- static void ThreadProc(Object stateInfo) {
- TaskInfo ti = (TaskInfo) stateInfo;
- Console.WriteLine(ti.Boilerplate, ti.Value);
- }
- }