首页新闻找找看学习计划

C#多线程操作list对象,急!!!!

0
悬赏园豆:10 [已解决问题] 解决于 2013-11-26 12:46

各位大神我如何把这六个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);
                }
            }
        }
发霉的荷尔蒙丶的主页 发霉的荷尔蒙丶 | 初学一级 | 园豆:5
提问于:2013-10-09 15:06
< >
分享
最佳答案
0

你在东方财富网工作?

收获园豆:10
Launcher | 高人七级 |园豆:45045 | 2013-10-09 16:31

为何如此发问?

jone_e | 园豆:1410 (小虾三级) | 2013-10-09 16:38

看到了,就不说了。

jone_e | 园豆:1410 (小虾三级) | 2013-10-09 16:41

@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();

Launcher | 园豆:45045 (高人七级) | 2013-10-09 16:49
其他回答(2)
0

list 多线程不安全吧 ,要用hashtable

哇~怪兽 | 园豆:603 (小虾三级) | 2013-10-09 15:49
0

如果你非要使用6个list的话,  吧6个list放到一个hashtable里面, 然后传入hashtable, 在List<StockInfo> list = (List<StockInfo>)code; 修改成对hashtable的分解就可以了。

zxwdlive | 园豆:7 (初学一级) | 2013-10-09 16:17
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册