c# 线程池如何同步请求并计算执行时间
今天写一个二进制文件查询耗时统计。
写完线程池,发现无法统计,全是异步执行。
经高人指点得知AutoRestEvent 类可以解决。但是随后又发现在AutoRestEvent.WriteAll()的时候,参数WriteHandle[]居然最大只支持64个元素。。。
果然又是以伏笔啊。显然高人并不是想给我全部写下来,还是想让我自己去查找和理解。。
不过还好不负厚望啊。。终于得以解决,下面贴出代码。予以分享:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Diagnostics;
using System.Net;
namespace ipfrom
{
public partial class Form1 : Form
{
Stopwatch logWatch = new Stopwatch();
Stopwatch datWatch = new Stopwatch();
DataTable dt=new DataTable();
private Thread logThread;
List<string> iplist = new List<string>();
private delegate void writeTxt(string txt);
private delegate void setBar(int v);
private delegate void setLabel(string str);
private int logMax = 10000;
private long extime = 0;
public Form1()
{
InitializeComponent();
//读取日志文件
//readLog();
}
private void setLabelText(string str)
{
if (this.textBox3.InvokeRequired)
{
setLabel sl = new setLabel(setLabelText);
this.BeginInvoke(sl, new object[] { str });
return;
}
this.textBox3.AppendText(str);
}
private void outtxt(string txt)
{
if (this.textBox2.InvokeRequired)
{
writeTxt wt = new writeTxt(outtxt);
this.BeginInvoke(wt,new object[]{txt});
return;
}
this.textBox2.AppendText(txt);
}
private void setBarValue(int v)
{
if (this.progressBar1.InvokeRequired)
{
setBar sb = new setBar(setBarValue);
this.BeginInvoke(sb, new object[] { v });
return;
}
this.progressBar1.Value = v;
}
private void readLog()
{
logWatch.Start();
StreamReader sr = new StreamReader(@"ex12092000.log");
sr.ReadLine();
sr.ReadLine();
sr.ReadLine();
sr.ReadLine();
sr.ReadLine();
for (int i = 0; i < (logMax); i++)
{
string ip = sr.ReadLine();
string[] ips=ip.Split(' ');
iplist.Add(ips[9]);
setBarValue(i+1);
}
sr.Close();
sr.Dispose();
logWatch.Stop();
TimeSpan tspan = logWatch.Elapsed;
double avgElapsed = logWatch.ElapsedMilliseconds / logMax;
setLabelText("执行:" + logMax.ToString()+ "次 耗时:" + logWatch.ElapsedMilliseconds + "毫秒 平均每次耗时:" + avgElapsed.ToString());
}
public void readDat(int start,int end)
{
datWatch.Start();
try
{
LookupService ls = new LookupService(@"GeoLiteCity.dat", LookupService.GEOIP_STANDARD);
//Location l = ls.getLocation(this.textBox1.Text.Trim());
for (int i = start; i < end; i++)
{
Location l = ls.getLocation(iplist[i]);
if (l != null)
{
string str = string.Format("ip:{9} [国家代码:{0} 国家名称: {1} 范围:{2} 城市:{3} 邮政编码:{4} 经纬度:{5},{6} 区域代码:{7} 范围代码:{8}]\r\n", l.countryCode, l.countryName, l.region, l.city, l.postalCode, l.latitude, l.longitude, l.area_code, l.regionName, iplist[i]);
//this.textBox2.AppendText(str);
outtxt(str);
}
else
{
//this.textBox2.Text = "IP Address Not Found\n";
outtxt("IP Address Not Found\n");
}
//setBarValue(i+1);
}
}
catch (System.Exception ez)
{
//this.textBox2.Text = "Error" + ez.Message + "\n";
outtxt("Error" + ez.Message + "\n");
}
//finally
//{
// ls.close();
//}
datWatch.Stop();
double avgElapsed = datWatch.ElapsedMilliseconds / logMax;
double avgSecond = datWatch.ElapsedMilliseconds / 1000;
setLabelText("耗时:" + datWatch.ElapsedMilliseconds + "毫秒 "+avgSecond+"秒 平均每次耗时:" + avgElapsed.ToString()+"毫秒\r\n");
}
public void readDat(object objs)
{
List<object> list = objs as List<object>;
string ip = list[0].ToString();
//AutoResetEvent are = list[1] as AutoResetEvent;
//ManualResetEvent are = list[1] as ManualResetEvent;
CountdownEvent cde = list[1] as CountdownEvent;
try
{
LookupService ls = new LookupService(@"GeoLiteCity.dat", LookupService.GEOIP_STANDARD);
//Location l = ls.getLocation(this.textBox1.Text.Trim());
//for (int i = start; i < end; i++)
{
Location l = ls.getLocation(ip);
if (l != null)
{
string str = string.Format("ip:{9} [国家代码:{0} 国家名称: {1} 范围:{2} 城市:{3} 邮政编码:{4} 经纬度:{5},{6} 区域代码:{7} 范围代码:{8}]\r\n", l.countryCode, l.countryName, l.region, l.city, l.postalCode, l.latitude, l.longitude, l.area_code, l.regionName, ip);
//this.textBox2.AppendText(str);
outtxt(str);
}
else
{
//this.textBox2.Text = "IP Address Not Found\n";
outtxt("IP Address Not Found\n");
}
//setBarValue(i+1);
}
}
catch (System.Exception ez)
{
//this.textBox2.Text = "Error" + ez.Message + "\n";
outtxt("Error" + ez.ToString() + "\n");
}
//are.Set();
cde.Signal();
//finally
//{
// ls.close();
//}
//double avgElapsed = datWatch.ElapsedMilliseconds / logMax;
// double avgSecond = datWatch.ElapsedMilliseconds / 1000;
//setLabelText("耗时:" + datWatch.ElapsedMilliseconds + "毫秒 " + avgSecond + "秒 平均每次耗时:" + avgElapsed.ToString() + "毫秒\r\n");
}
private void button1_Click(object sender, EventArgs e)
{
this.textBox3.Text = string.Empty;
this.progressBar1.Minimum = 1;
this.progressBar1.Maximum = logMax;
int avgReadCount = logMax / 20;
int start=0;
ThreadPool.SetMaxThreads(20, 20);
ThreadPool.SetMinThreads(1, 1);
datWatch.Start();
CountdownEvent cde = new CountdownEvent(iplist.Count);
int i = 0;
foreach (string ip in iplist)
{
List<object> objs=new List<object>();
objs.Add(ip);
objs.Add(cde);
WaitCallback wcb = new WaitCallback(readDat);
ThreadPool.QueueUserWorkItem(wcb,objs);
i++;
}
cde.Wait();
datWatch.Stop();
this.textBox3.Text = "耗时:" + datWatch.ElapsedMilliseconds.ToString();
//WaitCallback wcb = delegate { readDat(0, iplist.Count); };
//ThreadPool.QueueUserWorkItem(wcb);
//for (int i = 0; i < 20; i++)
//{
// //ThreadStart ipst = delegate { readDat(start, i * avgReadCount, i); };
// //ipThread = new Thread(ipst);
// //ipThread.Start();
// WaitCallback wcb = delegate { readDat(start,i*avgReadCount); };
// ThreadPool.QueueUserWorkItem(wcb);
// if (i >= 20)
// {
// start = 0;
// }
// else
// {
// start = i * avgReadCount;
// }
//}
//try
//{
// LookupService ls = new LookupService(@"D:\workspace\Test\IPSerchar\ipfrom\GeoLiteCity.dat", LookupService.GEOIP_STANDARD);
// for (int i = 0; i < iplist.Count; i++)
// {
// Location l = ls.getLocation(iplist[i]);
// if (l != null)
// {
// string str = string.Format("ip:{9} [国家代码:{0} 国家名称: {1} 范围:{2} 城市:{3} 邮政编码:{4} 经纬度:{5},{6} 区域代码:{7} 范围代码:{8}]\r\n", l.countryCode, l.countryName, l.region, l.city, l.postalCode, l.latitude, l.longitude, l.area_code, l.regionName,iplist[i]);
// this.textBox2.AppendText(str);
// }
// else
// {
// this.textBox2.Text = "IP Address Not Found\n";
// }
// }
//}
//catch (System.Exception ez)
//{
// this.textBox2.Text="Error" + ez.Message + "\n";
//}
}
private void button2_Click(object sender, EventArgs e)
{
logMax = Int32.Parse(this.textBox1.Text.Trim());
this.progressBar1.Minimum = 1;
this.progressBar1.Maximum = logMax;
logThread = new Thread(new ThreadStart(readLog));
logThread.Start();
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Diagnostics;
using System.Net;
namespace ipfrom
{
public partial class Form1 : Form
{
Stopwatch logWatch = new Stopwatch();
Stopwatch datWatch = new Stopwatch();
DataTable dt=new DataTable();
private Thread logThread;
List<string> iplist = new List<string>();
private delegate void writeTxt(string txt);
private delegate void setBar(int v);
private delegate void setLabel(string str);
private int logMax = 10000;
private long extime = 0;
public Form1()
{
InitializeComponent();
//读取日志文件
//readLog();
}
private void setLabelText(string str)
{
if (this.textBox3.InvokeRequired)
{
setLabel sl = new setLabel(setLabelText);
this.BeginInvoke(sl, new object[] { str });
return;
}
this.textBox3.AppendText(str);
}
private void outtxt(string txt)
{
if (this.textBox2.InvokeRequired)
{
writeTxt wt = new writeTxt(outtxt);
this.BeginInvoke(wt,new object[]{txt});
return;
}
this.textBox2.AppendText(txt);
}
private void setBarValue(int v)
{
if (this.progressBar1.InvokeRequired)
{
setBar sb = new setBar(setBarValue);
this.BeginInvoke(sb, new object[] { v });
return;
}
this.progressBar1.Value = v;
}
private void readLog()
{
logWatch.Start();
StreamReader sr = new StreamReader(@"ex12092000.log");
sr.ReadLine();
sr.ReadLine();
sr.ReadLine();
sr.ReadLine();
sr.ReadLine();
for (int i = 0; i < (logMax); i++)
{
string ip = sr.ReadLine();
string[] ips=ip.Split(' ');
iplist.Add(ips[9]);
setBarValue(i+1);
}
sr.Close();
sr.Dispose();
logWatch.Stop();
TimeSpan tspan = logWatch.Elapsed;
double avgElapsed = logWatch.ElapsedMilliseconds / logMax;
setLabelText("执行:" + logMax.ToString()+ "次 耗时:" + logWatch.ElapsedMilliseconds + "毫秒 平均每次耗时:" + avgElapsed.ToString());
}
public void readDat(int start,int end)
{
datWatch.Start();
try
{
LookupService ls = new LookupService(@"GeoLiteCity.dat", LookupService.GEOIP_STANDARD);
//Location l = ls.getLocation(this.textBox1.Text.Trim());
for (int i = start; i < end; i++)
{
Location l = ls.getLocation(iplist[i]);
if (l != null)
{
string str = string.Format("ip:{9} [国家代码:{0} 国家名称: {1} 范围:{2} 城市:{3} 邮政编码:{4} 经纬度:{5},{6} 区域代码:{7} 范围代码:{8}]\r\n", l.countryCode, l.countryName, l.region, l.city, l.postalCode, l.latitude, l.longitude, l.area_code, l.regionName, iplist[i]);
//this.textBox2.AppendText(str);
outtxt(str);
}
else
{
//this.textBox2.Text = "IP Address Not Found\n";
outtxt("IP Address Not Found\n");
}
//setBarValue(i+1);
}
}
catch (System.Exception ez)
{
//this.textBox2.Text = "Error" + ez.Message + "\n";
outtxt("Error" + ez.Message + "\n");
}
//finally
//{
// ls.close();
//}
datWatch.Stop();
double avgElapsed = datWatch.ElapsedMilliseconds / logMax;
double avgSecond = datWatch.ElapsedMilliseconds / 1000;
setLabelText("耗时:" + datWatch.ElapsedMilliseconds + "毫秒 "+avgSecond+"秒 平均每次耗时:" + avgElapsed.ToString()+"毫秒\r\n");
}
public void readDat(object objs)
{
List<object> list = objs as List<object>;
string ip = list[0].ToString();
//AutoResetEvent are = list[1] as AutoResetEvent;
//ManualResetEvent are = list[1] as ManualResetEvent;
CountdownEvent cde = list[1] as CountdownEvent;
try
{
LookupService ls = new LookupService(@"GeoLiteCity.dat", LookupService.GEOIP_STANDARD);
//Location l = ls.getLocation(this.textBox1.Text.Trim());
//for (int i = start; i < end; i++)
{
Location l = ls.getLocation(ip);
if (l != null)
{
string str = string.Format("ip:{9} [国家代码:{0} 国家名称: {1} 范围:{2} 城市:{3} 邮政编码:{4} 经纬度:{5},{6} 区域代码:{7} 范围代码:{8}]\r\n", l.countryCode, l.countryName, l.region, l.city, l.postalCode, l.latitude, l.longitude, l.area_code, l.regionName, ip);
//this.textBox2.AppendText(str);
outtxt(str);
}
else
{
//this.textBox2.Text = "IP Address Not Found\n";
outtxt("IP Address Not Found\n");
}
//setBarValue(i+1);
}
}
catch (System.Exception ez)
{
//this.textBox2.Text = "Error" + ez.Message + "\n";
outtxt("Error" + ez.ToString() + "\n");
}
//are.Set();
cde.Signal();
//finally
//{
// ls.close();
//}
//double avgElapsed = datWatch.ElapsedMilliseconds / logMax;
// double avgSecond = datWatch.ElapsedMilliseconds / 1000;
//setLabelText("耗时:" + datWatch.ElapsedMilliseconds + "毫秒 " + avgSecond + "秒 平均每次耗时:" + avgElapsed.ToString() + "毫秒\r\n");
}
private void button1_Click(object sender, EventArgs e)
{
this.textBox3.Text = string.Empty;
this.progressBar1.Minimum = 1;
this.progressBar1.Maximum = logMax;
int avgReadCount = logMax / 20;
int start=0;
ThreadPool.SetMaxThreads(20, 20);
ThreadPool.SetMinThreads(1, 1);
datWatch.Start();
CountdownEvent cde = new CountdownEvent(iplist.Count);
int i = 0;
foreach (string ip in iplist)
{
List<object> objs=new List<object>();
objs.Add(ip);
objs.Add(cde);
WaitCallback wcb = new WaitCallback(readDat);
ThreadPool.QueueUserWorkItem(wcb,objs);
i++;
}
cde.Wait();
datWatch.Stop();
this.textBox3.Text = "耗时:" + datWatch.ElapsedMilliseconds.ToString();
//WaitCallback wcb = delegate { readDat(0, iplist.Count); };
//ThreadPool.QueueUserWorkItem(wcb);
//for (int i = 0; i < 20; i++)
//{
// //ThreadStart ipst = delegate { readDat(start, i * avgReadCount, i); };
// //ipThread = new Thread(ipst);
// //ipThread.Start();
// WaitCallback wcb = delegate { readDat(start,i*avgReadCount); };
// ThreadPool.QueueUserWorkItem(wcb);
// if (i >= 20)
// {
// start = 0;
// }
// else
// {
// start = i * avgReadCount;
// }
//}
//try
//{
// LookupService ls = new LookupService(@"D:\workspace\Test\IPSerchar\ipfrom\GeoLiteCity.dat", LookupService.GEOIP_STANDARD);
// for (int i = 0; i < iplist.Count; i++)
// {
// Location l = ls.getLocation(iplist[i]);
// if (l != null)
// {
// string str = string.Format("ip:{9} [国家代码:{0} 国家名称: {1} 范围:{2} 城市:{3} 邮政编码:{4} 经纬度:{5},{6} 区域代码:{7} 范围代码:{8}]\r\n", l.countryCode, l.countryName, l.region, l.city, l.postalCode, l.latitude, l.longitude, l.area_code, l.regionName,iplist[i]);
// this.textBox2.AppendText(str);
// }
// else
// {
// this.textBox2.Text = "IP Address Not Found\n";
// }
// }
//}
//catch (System.Exception ez)
//{
// this.textBox2.Text="Error" + ez.Message + "\n";
//}
}
private void button2_Click(object sender, EventArgs e)
{
logMax = Int32.Parse(this.textBox1.Text.Trim());
this.progressBar1.Minimum = 1;
this.progressBar1.Maximum = logMax;
logThread = new Thread(new ThreadStart(readLog));
logThread.Start();
}
}
}
有些人贴代码不喜欢贴全,我这人就喜欢贴全。。。大家可以看下
浙公网安备 33010602011771号