private void DownLoadDork(object obj) { DownLoadParams dparm = obj as DownLoadParams; foreach (var item in dparm.downLoadInfo) { DownLoad(item); } }
public string Start(List <DownLoadInfo> list) { var pathsCount = list.Count; //资源总数 // 计算编译线程数量 int threadCount = pathsCount % Number100 == 0 ? pathsCount / Number100 : pathsCount / Number100 + 1; if (threadCount > MaxThreadCount) { threadCount = MaxThreadCount; } int threadPqgeSize = (pathsCount / threadCount) + 1; int typeSum = 0; List <DownLoadParams> parameters = new List <DownLoadParams>(threadCount); DownLoadInfo[] downloadParam = null; DownLoadParams downLoadParams = null; int index, endSize = 0;; for (int i = 0; i < threadCount; i++) { downLoadParams = new DownLoadParams(); endSize = threadPqgeSize * (i + 1); if (endSize > pathsCount) { var a = threadPqgeSize + pathsCount - endSize; downloadParam = new DownLoadInfo[threadPqgeSize + pathsCount - endSize]; endSize = pathsCount; } else { downloadParam = new DownLoadInfo[threadPqgeSize]; } index = 0; for (int j = typeSum; j < endSize; j++) { downloadParam[index++] = list[j]; } downLoadParams.downLoadInfo = downloadParam; typeSum += downloadParam.Count(); parameters.Add(downLoadParams); } // 创建编译线程 List <Thread> threads = new List <Thread>(threadCount); for (int i = 1; i < threadCount; i++) { Thread thread = new Thread(DownLoadDork); thread.IsBackground = true; thread.Name = "DownloadThread #" + i.ToString(); threads.Add(thread); thread.Start(parameters[i]); } // 重用当前线程:为当前线程指派下载任务。 DownLoadDork(parameters[0]); // 等待所有的下载线程执行线束。 foreach (Thread thread in threads) { thread.Join(); } return("下载完成"); }