主要代码: 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 EWin.International.PolicyUpload.common; using System.Collections; using System.Threading; using System.Globalization; namespace test { public partial class Form2 : Form { //private Dictionary<List<string>, Thread> _ThreadDictionary = new Dictionary<List<string>, Thread>(); //存储采集任务线程 private int _MaxThreadCount = 4; private static int _intErrors = 0; private static int _intCorrects = 0; private static int _intSecent = 0; private static object thisLock = new object(); private delegate void UpdateStatusDelegate(string status); private void UpdateStatus(string status) { this.textBox1.AppendText("\r\n" + status); } private void msgshow(string msg) { this.BeginInvoke(new UpdateStatusDelegate(UpdateStatus), new object[] { string.Format("{0}", msg) }); } public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { ArrayList mylist = new ArrayList(); mylist.Add(new DictionaryEntry("单程1", "1")); mylist.Add(new DictionaryEntry("往返2", "2")); cbb_triptype.DataSource = mylist; cbb_triptype.DisplayMember = "Key"; cbb_triptype.ValueMember = "Value"; mylist = new ArrayList(); mylist.Add(new DictionaryEntry("TravelAgentSearch", "TravelAgentSearch")); mylist.Add(new DictionaryEntry("XinMinGSearch", "XinMinGSearch")); cbb_InterfaceType.DataSource = mylist; cbb_InterfaceType.DisplayMember = "Key"; cbb_InterfaceType.ValueMember = "Value"; //Form.CheckForIllegalCrossThreadCalls = false; } private void btn_ensure_Click(object sender, EventArgs e) { _intErrors = 0; _intCorrects = 0; textBox1.Text = ""; string strtriptyp = cbb_triptype.SelectedValue.ToString(); strtriptyp += ";" + cbb_InterfaceType.SelectedValue.ToString(); ThreadPool.QueueUserWorkItem(new WaitCallback(this.dowork), strtriptyp); } private void dowork(object obj) { int intmaxcount = string.IsNullOrEmpty(txt_maxcount.Text) ? 20 : int.Parse(txt_maxcount.Text); //并发 int inttimes = string.IsNullOrEmpty(txt_times.Text) ? 10 : int.Parse(txt_times.Text);//执行次数 _intSecent = string.IsNullOrEmpty(txt_secend.Text) ? 1 : int.Parse(txt_secend.Text);//单位时间(毫秒 List<string> listobj = new List<string>(); DateTime dtfdate = DateTime.ParseExact(txt_fdate.Text, "yyyyMMdd", CultureInfo.InvariantCulture); string strobj = obj as string; string strtriptyp = strobj.Split(‘;‘)[0]; string strinterfacetype = strobj.Split(‘;‘)[1]; msgshow("==" + strinterfacetype + "测试=="); DateTime dtrdate = DateTime.Now; if (strtriptyp == "2") { dtrdate = DateTime.ParseExact(txt_rdate.Text, "yyyyMMdd", CultureInfo.InvariantCulture); } msgshow("==begin==每" + _intSecent.ToString() + "秒并发" + intmaxcount.ToString() + ";共执行" + inttimes + "次====" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); for (int i = 0; i < inttimes; i++) //执行次数 { msgshow("第" + (i + 1).ToString() + "次开始执行==" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); //并发次数 行程类型 始发 到达 始发日期 返回日期 当前执行序号 listobj = new List<string>(); listobj.Add(intmaxcount.ToString() + "|" + strtriptyp + "|" + txt_statcity.Text + "|" + txt_endcity.Text + "|" + dtfdate.ToString("yyyyMMdd") + "|" + ((string.IsNullOrEmpty(txt_rdate.Text) && strtriptyp == "2") ? "" : dtrdate.ToString("yyyyMMdd")) + "|" + (i + 1).ToString() + "|" + strinterfacetype); ThreadPool.QueueUserWorkItem(new WaitCallback(SearchHandler), listobj); Thread.Sleep(_intSecent * 1000); //common.test tt = new common.test(); //ThreadPool.QueueUserWorkItem(new WaitCallback(tt.TravelAgentSearch), intmaxcount.ToString() + "|" + strtriptyp + "|" + txt_statcity.Text + "|" + txt_endcity.Text + "|" + dtfdate.ToString("yyyyMMdd") + "|" + ((string.IsNullOrEmpty(txt_rdate.Text) && strtriptyp == "2") ? "" : dtrdate.ToString("yyyyMMdd")) + "|" + (i + 1).ToString() + "|" + strinterfacetype); //dtfdate = dtfdate.AddDays(1); ////msgshow("Thread.Sleep:" + (_intSecent * 1000).ToString() + "毫秒"); } msgshow("==end==每" + _intSecent.ToString() + "秒并发" + intmaxcount.ToString() + ";共执行" + inttimes + "次====" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\r\n"); } private void SearchHandler(object obj) { //Thread.Sleep(_intSecent * 1000); //msgshow("Thread.Sleep:" + (_intSecent* 1000).ToString() + "毫秒"); //listobj:并发次数 行程类型 始发 到达 始发日期 返回日期 List<string> listobj = obj as List<string>; string[] objlist = listobj[0].Split(‘|‘); int intmaxcount = int.Parse(objlist[0].ToString()); _MaxThreadCount = intmaxcount; string stringtriptyp = objlist[1].ToString(); string strstatcity = objlist[2].ToString(); string strendcity = objlist[3].ToString(); DateTime dtfdate = DateTime.ParseExact(objlist[4].ToString(), "yyyyMMdd", CultureInfo.InvariantCulture); DateTime dtrdate = DateTime.Now; if (!string.IsNullOrEmpty(objlist[5])) dtrdate = DateTime.ParseExact(objlist[5].ToString(), "yyyyMMdd", CultureInfo.InvariantCulture); Dictionary<List<string>, Thread> ThreadDictionary = new Dictionary<List<string>, Thread>(); List<string> list; for (int i = 0; i < intmaxcount; i++) { list = new List<string>(); DateTime dtnewfdate = dtfdate.AddDays(i); DateTime dtnewrdate = dtrdate.AddDays(i); //行程类型 始发 到达 始发日期 返回日期 list.Add(stringtriptyp); list.Add(strstatcity); list.Add(strendcity); list.Add(dtnewfdate.ToString("yyyyMMdd")); list.Add((!string.IsNullOrEmpty(objlist[5].ToString()) && stringtriptyp == "2") ? dtnewrdate.ToString("yyyyMMdd") : null); list.Add(objlist[6].ToString()); if (objlist[7].ToString() == "TravelAgentSearch") { ThreadDictionary.Add(list, new Thread(new ParameterizedThreadStart(TravelAgentSearch), 5)); } else { ThreadDictionary.Add(list, new Thread(new ParameterizedThreadStart(XinMinGSearch), 5)); } } ThreadPool.QueueUserWorkItem(new WaitCallback(this.TravelAgentSearchAll), ThreadDictionary); } private void TravelAgentSearchAll(object obj) { DateTime opTime = DateTime.Now; //msgshow("==开始==" + opTime.ToString("yyyy-MM-dd HH:mm:ss.fff")); //LogUtils.Info("==开始==" + opTime.ToString("yyyy-MM-dd HH:mm:ss.fff")); Dictionary<List<string>, Thread> ThreadDictionary = obj as Dictionary<List<string>, Thread>; #region 线程队列处理 if (ThreadDictionary.Count > 0) { int maxThreadSize = _MaxThreadCount; //最大并发线程数量 int totalThreadCount = ThreadDictionary.Count; //总线程数量 { int worksThreadSize = ThreadDictionary.Where(k => k.Value.IsAlive).Count(); //当前活动线程数量 if (worksThreadSize < maxThreadSize) { foreach (KeyValuePair<List<string>, Thread> item in ThreadDictionary.Where(k => k.Value.ThreadState.Equals(ThreadState.Unstarted))) { item.Value.Start(item.Key); worksThreadSize++; totalThreadCount--; //达到最大活跃线程数量则跳出 Foreach 循环 , 重新执行 while 中代码. if (worksThreadSize == maxThreadSize) break; } } } //等待所有线程操作完成 while (ThreadDictionary.Where(k => k.Value.IsAlive).Count() > 0) { } List<string> list = ThreadDictionary.Keys.LastOrDefault() as List<string>; msgshow("==第" + list[5] + "次======开始时间:" + opTime.ToString("yyyy-MM-dd HH:mm:ss.fff") + "结束时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "耗时:" + DateTime.Now.Subtract(opTime).TotalSeconds + "毫秒\n"); msgshow("当前执行了" + (_intCorrects + _intErrors).ToString() + "次;" + "正确" + _intCorrects.ToString() + "次;" + "错误" + _intErrors.ToString() + "次==" + "成功比率:" + ((float)_intCorrects / (_intCorrects + _intErrors)).ToString()); LogUtils.Info("========开始时间:" + opTime.ToString("yyyy-MM-dd HH:mm:ss.fff") + "结束时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "耗时:" + DateTime.Now.Subtract(opTime).TotalSeconds + "毫秒\n"); } #endregion } //TravelAgent private void TravelAgentSearch(object obj) { List<string> list = obj as List<string>; TravelAgentService.TravelAgentServiceSoapClient client = new TravelAgentService.TravelAgentServiceSoapClient(); TravelAgentService.SearchRequest model = new TravelAgentService.SearchRequest(); model.cid = "cid"; model.tripType = list[0]; model.fromCity = list[1]; model.toCity = list[2]; model.fromDate = list[3]; model.retDate = string.IsNullOrEmpty(list[4]) ? null : list[4]; try { LogUtils.Info("请求参数:" + Newtonsoft.Json.JsonConvert.SerializeObject(model)); TravelAgentService.SearchResult rsp = client.Search(model); lock (thisLock) { _intCorrects++; } LogUtils.Info("响应数据:" + Newtonsoft.Json.JsonConvert.SerializeObject(rsp)); } catch (Exception ex) { lock (thisLock) { _intErrors++; msgshow(string.Format("当前出错总次数:{0};", _intErrors.ToString())); LogUtils.Info(string.Format("出错:{0};发送请求参数{1}", ex.Message, Newtonsoft.Json.JsonConvert.SerializeObject(model))); LogUtils.Info(string.Format("当前出错总次数:{0};", _intErrors.ToString())); } } } //XinMinG private void XinMinGSearch(object obj) { List<string> list = obj as List<string>; XiMinGService.XiMinGServiceSoapClient client = new XiMinGService.XiMinGServiceSoapClient(); XiMinGService.SearchRequest model = new XiMinGService.SearchRequest(); model.UName = "UName"; model.UPassword = "upwd"; model.tripType = list[0]; model.fromCity = list[1]; model.toCity = list[2]; model.fromDate = list[3]; model.retDate = string.IsNullOrEmpty(list[4]) ? null : list[4]; try { LogUtils.Info("请求参数:" + Newtonsoft.Json.JsonConvert.SerializeObject(model)); XiMinGService.ListResponseOfT_PolicyModel rsp = client.Search(model); lock (thisLock) { _intCorrects++; } LogUtils.Info("响应数据:" + Newtonsoft.Json.JsonConvert.SerializeObject(rsp)); } catch (Exception ex) { lock (thisLock) { _intErrors++; LogUtils.Info(string.Format("出错:{0};发送请求参数{1}", ex.Message, Newtonsoft.Json.JsonConvert.SerializeObject(model))); msgshow(string.Format("当前出错总次数:{0};", _intErrors.ToString())); LogUtils.Info(string.Format("当前出错总次数:{0};", _intErrors.ToString())); } } } } }
前台页面截图:
参考:
http://blog.sina.com.cn/s/blog_4dde37650100oh91.html
时间: 2024-10-29 04:32:24