首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#多线程操作list对象,该怎么处理

2013-10-13 
C#多线程操作list对象,急!!!!各位大神我如何把这六个list对象同时传入ReadUrlContent这个方法,并能同时启

C#多线程操作list对象,急!!!!
各位大神我如何把这六个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);
                }
            }
        }

多线程 c# httpwebrequest 对象
[解决办法]
全部传进去遍历
List<List<StockInfo>> list = (List<List<StockInfo>>)code;
[解决办法]
这跟多线程没什么关系吧。

你用List<List<StockInfo>> resultLists = new List<List<StockInfo>>();
然后,resultLists .Add(results1), results2等等。

把resultLists 传参过去。

static void ReadUrlContent(object code)方法中 

List<List<StockInfo>> lists = (List<List<StockInfo>> )code;
[解决办法]
 ..............

Thread thread = new Thread(new ParameterizedThreadStart(ReadUrlContent));
 thread.Start(new object[] { result1, result2, result3, result4, result5});

static void ReadUrlContent(object code)
{
           object[] objs = code as object[];
            List<StockInfo> result1= (List<StockInfo>)objs[0];
            List<StockInfo> result2= (List<StockInfo>)objs[1];
           List<StockInfo> result3=  (List<StockInfo>)objs[2];
           List<StockInfo> result4 = (List<StockInfo>)objs[3];
}

直接复制用 给你改了

热点排行