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#多线程List的非线程安全性
最近在做多线程方面的工作,工作中发现多线程中很多坑,这里就有一个List添加对象的误区,解释了List容量扩展后内存开辟导致的线程不安全性的问题,这里做个分享跟大家讲讲这个坑是怎么形成的怎么样避免。
C# 修改list所有对象中的某字段
C# 修改list所有对象中的某字段 博客地址:https://blog.csdn.net/qq_36051316/article/details/83475372 的案例
C# list对象FindAll函数的四种写法
详细介绍了C#中list对象的FindAll函数4种写法,从中可以看出如何从最完整写法过渡到最简写法的。从中可以明白C#“=>”的含义。
多线程及list使用.zip
多线程及list集合的基本使用,可添加元素、遍历元素、删除元素
c# 委托访问listbox多线程操作
此文档提供了一个委托方法,来控制访问listbox,用于多线程操作。c#


