ThreadPool之SmartThreadPool应用

参考资料:http://hi.baidu.com/cdmember_daihw/blog/item/26e40a86d4ed7b3866096e49.html

1: SmartThreadPool介绍:http://www.codeproject.com/KB/threads/smartthreadpool.aspx
2: 下载SmartThreadPool——http://smartthreadpool.codeplex.com/
3:在VS中添加引用:SmartThreadPool.dll,其中dll有几个版本,找到项目对应的DLL。
4:创建socket
public Socket ListenerClassFromSmartThreadPool()
{
//建立socket偵聽連接
listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
IPEndPoint locEP = new IPEndPoint(IPAddress.Any, 2000);
listener.Bind(locEP);
listener.Listen(100);

STPStartInfo stp = new STPStartInfo();
stp.DisposeOfStateObjects = true;
stp.CallToPostExecute = CallToPostExecute.Always;
stp.ThreadPriority = ThreadPriority.Highest;
stp.UseCallerCallContext = true;
stp.MaxWorkerThreads = 100;
stp.IdleTimeout = 10;

SmartThreadPool smart = new SmartThreadPool(stp);
smart.Start();

//SmartThreadPool smart = new SmartThreadPool();
//smart.Start();

IWorkItemResult result = smart.QueueWorkItem(new WorkItemCallback(this.ClientWorkThreadFromSmartThreadPool), null);

//SmartThreadPool.WaitAll(new IWorkItemResult[] { result });
smart.WaitForIdle();
/* 其中 注意WaitAll 、WaitAny、WaitForIdle之间关系 */

//Console.WriteLine(result.Result);
//smart.Shutdown();
//建立一个线程池
//ThreadPool.QueueUserWorkItem(new WaitCallback(ClientWorkThread));
}
catch (Exception ex)
{
help2.logWrite("建立监控出错:" + ex.Message);
}
return listener;
}
5: socket中具体过程,注意:SmartThreadPool要求调用过程返值是object.而.net自带要求不带返回值
private object ClientWorkThreadFromSmartThreadPool(Object stateInfo)
{
ClientWorkThread(null);
return null;
}

//处理连接请求
private void ClientWorkThread(Object stateInfo)
{
byte[] buffer = new byte[8192];

try
{
while (true)
{
try
{
Socket socket = listener.Accept();
IPEndPoint remoEP = ((IPEndPoint)socket.RemoteEndPoint);
string recString = "接收到来自" + remoEP.Address.ToString() + "的连接。";

//Console.WriteLine(recString);

int receCount = socket.Receive(buffer);

if (receCount > 0)
{
//string recievestring = Encoding.Default.GetString(buffer, 0, receCount);
string recievestring = Encoding.GetEncoding("gb2312").GetString(buffer, 0, receCount);

//recievestring = "sdfsdfs:" + recievestring;
if (recievestring != "")
{
recievestring = GetUserAuth(recievestring);

//byte[] bytestr = System.Text.Encoding.Unicode.GetBytes(str);
//byte[] sendbuffer = new byte[8192];

//sendbuffer = Encoding.ASCII.GetBytes(recievestring);

byte[] sendbuffer = Encoding.GetEncoding("gb2312").GetBytes(recievestring);

recString = "来自客户端" + remoEP.Address.ToString() + "的消息:" + recievestring;

//Console.WriteLine(recString);

AddListItemHandler addhandle = new AddListItemHandler(AddListItem);
addhandle.Invoke(recString); //调用窗体来执行委托

socket.Send(sendbuffer, sendbuffer.Length, SocketFlags.None);
}
}
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
catch (Exception ex)
{
help2.logWrite("接收信息出错_1,循环停摆1分钟:" + ex.Message);
System.Threading.Thread.Sleep(60000);
}
}
}
catch (Exception ex)
{
help2.logWrite("接收信息出错:" + ex.Message);
}
}


转自:http://hillstone.blog.hexun.com/54716371_d.html
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值