using System;
using System.Threading;
public class Example {
public static void Main() {
// Queue the task.
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc2));
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.ReadLine();
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.
for(int i=0;i<50;i++)
{
Random rnd = new Random();
int n = rnd.Next(100,1000);
Thread.Sleep(n);
Console.WriteLine("AAAAAA."+i.ToString());
}
}
static void ThreadProc2(Object stateInfo) {
// No state object was passed to QueueUserWorkItem, so
// stateInfo is null.
for(int i=0;i<50;i++)
{
Random rnd = new Random();
int n = rnd.Next(100,1000);
Thread.Sleep(n);
Console.WriteLine("BBBB."+i.ToString());
}
}
}
using System.Threading;
public class Example {
public static void Main() {
// Queue the task.
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc2));
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.ReadLine();
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.
for(int i=0;i<50;i++)
{
Random rnd = new Random();
int n = rnd.Next(100,1000);
Thread.Sleep(n);
Console.WriteLine("AAAAAA."+i.ToString());
}
}
static void ThreadProc2(Object stateInfo) {
// No state object was passed to QueueUserWorkItem, so
// stateInfo is null.
for(int i=0;i<50;i++)
{
Random rnd = new Random();
int n = rnd.Next(100,1000);
Thread.Sleep(n);
Console.WriteLine("BBBB."+i.ToString());
}
}
}
转载别人的一个类..
using System;
using System.Threading ;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Collections ;
using System.Text ;
using System.Diagnostics ;
namespace Bigeagle.Net.Server
{
/// <summary>
/// <br>server类</br>
/// <br>Author: bigeagle@163.net</br>
/// <br>Date : 2002/02/04</br>
/// <br>History: 2002/02/04 finished</br>
/// <br>Todo: 现在的类结构不尽合理,将来考虑使用delgates以便同winform绑定。</br>
/// </summary>
/// <remarks>
/// <br>抽象类,用于一些常见的网络server的基类,如http,ftp,mail等。</br>
/// <br>主要功能是监听一个端口,然后利用线程池起规定数量的线程等待客户端连接
/// ,同时有一个守护线程监视线程使用情况,如果所有线程都在工作,则起一个空闲线程
/// 继续等待客户端的连接。
/// </br>
/// <br>子类继承后必须实现其OnCommunication()抽象方法来完成自己要做的工作。</br>
/// </remarks>
public abstract class Server
{
常量
静态变量
成员变量定义
属性定义
构造函数
私有函数
保护函数
公共方法
}//end class
}//end namespace
/Files/lovebanyi/threadpool.rar
using System.Threading ;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Collections ;
using System.Text ;
using System.Diagnostics ;
namespace Bigeagle.Net.Server
{
/// <summary>
/// <br>server类</br>
/// <br>Author: bigeagle@163.net</br>
/// <br>Date : 2002/02/04</br>
/// <br>History: 2002/02/04 finished</br>
/// <br>Todo: 现在的类结构不尽合理,将来考虑使用delgates以便同winform绑定。</br>
/// </summary>
/// <remarks>
/// <br>抽象类,用于一些常见的网络server的基类,如http,ftp,mail等。</br>
/// <br>主要功能是监听一个端口,然后利用线程池起规定数量的线程等待客户端连接
/// ,同时有一个守护线程监视线程使用情况,如果所有线程都在工作,则起一个空闲线程
/// 继续等待客户端的连接。
/// </br>
/// <br>子类继承后必须实现其OnCommunication()抽象方法来完成自己要做的工作。</br>
/// </remarks>
public abstract class Server
{
常量
静态变量
成员变量定义
属性定义
构造函数
私有函数
保护函数
公共方法
}//end class
}//end namespace