C#/.NET多线程实战——自定义程序运行的线程,并保持线程数不变
作者:互联网
C#/.NET多线程实战——自定义程序运行的线程,并保持线程数不变
1.无参数
int asynLimit = 15;//异步线程限制
List<Task> taskList = new List<Task>();
TaskFactory taskFactory = new TaskFactory();
foreach (Category category in categoryList)
{
ISearch searcher = new CommoditySearch(category);
//searcher.Crawler();
taskList.Add(taskFactory.StartNew(searcher.Crawler));
if (taskList.Count > asynLimit )
{
taskList = taskList.Where(t => !t.IsCompleted && !t.IsCanceled && !t.IsFaulted).ToList();
Task.WaitAny(taskList.ToArray());
}
}
1.有参数
int asynLimit = 15;//异步线程限制
List<Task> taskList = new List<Task>();
TaskFactory taskFactory = new TaskFactory();
foreach (Category category in categoryList)
{
ISearch searcher = new CommoditySearch(category);
//searcher.Crawler();
taskList.Add(taskFactory.StartNew(new Action<object>(DownLoadAndSaveImg), new List<string>() { item,models.Title,index.ToString()}));
if (taskList.Count > asynLimit )
{
taskList = taskList.Where(t => !t.IsCompleted && !t.IsCanceled && !t.IsFaulted).ToList();
Task.WaitAny(taskList.ToArray());
}
}
标签:searcher,taskList,自定义,asynLimit,List,线程,new,多线程 来源: https://blog.csdn.net/qq_34202873/article/details/88753269