各位大神我如何把这六个list对象同时传入ReadUrlContent这个方法,并能同时启用,以下是代码请大家帮忙看看啊
List<StockInfo> result1 = new List<StockInfo>(); List<StockInfo> result2 = new List<StockInfo>(); List<StockInfo> result3 = new List<StockInfo>(); List<StockInfo> result4 = new List<StockInfo>(); List<StockInfo> result5 = new List<StockInfo>(); List<StockInfo> result6 = new List<StockInfo>(); Console.WriteLine("begin1:" + DateTime.Now.ToString()); Thread thread = new Thread(new ParameterizedThreadStart(ReadUrlContent)); Thread.Sleep(300); Console.WriteLine("begin1启动"+DateTime.Now.ToString()); thread.Start(result1); Console.WriteLine("end1启动" + DateTime.Now.ToString()); Thread thread1 = new Thread(new ParameterizedThreadStart(ReadUrlContent)); Thread.Sleep(300); Console.WriteLine("begin2启动" + DateTime.Now.ToString()); thread.Start(result2); Console.WriteLine("end2启动" + DateTime.Now.ToString()); Console.WriteLine("end1:" + DateTime.Now.ToString()); static void ReadUrlContent(object code) { List<StockInfo> list = (List<StockInfo>)code; foreach (var item in list) { try { StringBuilder sb = new StringBuilder(); byte[] buf = new byte[8192]; HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://dataapi.eastmoney.com:8080/bbsj/stock.aspx?code=" + code.ToString() + ""); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream resStream = response.GetResponseStream(); string tempString = null; int totalcount = 0; int count = 0; using (FileStream fs = new FileStream(@"D:\eastmoney\data\bbsj\page\" + item.StockCode + ".html", FileMode.OpenOrCreate, FileAccess.Write)) { do { count = resStream.Read(buf, 0, buf.Length); if (count != 0) { tempString = Encoding.ASCII.GetString(buf, 0, count); fs.Write(buf, 0, count); } totalcount += count; } while (count > 0); resStream.Close(); response.Close(); fs.Close(); } } catch (Exception ex) { Console.WriteLine(ex.Message + code); } } }
你在东方财富网工作?
为何如此发问?
看到了,就不说了。
@jone_e:
List<List<StockInfo>> results = new List<List<StockInfo>>();
List<Thread> workers = new List<Thread>();
for(int i=0;i<6;i++)
{
results[i] = new List<StockInfo>();
workers[i] = new Thread(new ParameterizedThreadStart(ReadUrlContent));
workers[i].Start(results[i]);
}
for(int i=0;i<6;i++)
workers[i].Join();
list 多线程不安全吧 ,要用hashtable
如果你非要使用6个list的话, 吧6个list放到一个hashtable里面, 然后传入hashtable, 在List<StockInfo> list = (List<StockInfo>)code; 修改成对hashtable的分解就可以了。