public void EnqueueNormalTask(HttpDownTask task) { lock (downloadQueue) { downloadQueue.Enqueue(task); } }
public long DownLoadOneFile(string fileUrl, string savePath, bool highPriority = false, bool erase = false) { HttpWebRequest request = HttpClient.getWebRequest(fileUrl, 0); WebResponse response = request.GetResponse(); long fileLength = response.ContentLength; long taskId = GenerateTaskId(); HttpDownTask downloadTask = new HttpDownTask(taskId, fileUrl, savePath, fileLength, erase); if (true == highPriority) { EnqueueHighPriorityTask(downloadTask); } else { EnqueueNormalTask(downloadTask); } // 添加到所有任务列表 allTaskMap.Add(taskId, downloadTask); request.Abort(); response.Close(); return(taskId); }
public void EnqueueHighPriorityTask(HttpDownTask task) { lock (highPriorityQueue) { highPriorityQueue.Enqueue(task); } }
public long DownLoadOneFile(string fileUrl, string savePath, int totalLength, bool highPriority = false, bool erase = false) { long fileLength = totalLength; long taskId = GenerateTaskId(); HttpDownTask downloadTask = new HttpDownTask(taskId, fileUrl, savePath, fileLength, erase); if (true == highPriority) { EnqueueHighPriorityTask(downloadTask); } else { EnqueueNormalTask(downloadTask); } // 添加到所有任务列表 allTaskMap.Add(taskId, downloadTask); return(taskId); }
public void DownloadCheck() { //List<Thread> finishList = new List<Thread>(); List <HttpDownTask> finishList = new List <HttpDownTask>(); while (true) { //foreach(KeyValuePair<Thread, HttpDownTask> pair in downloadThreadMap) //{ // Thread downloadThread = pair.Key; // if((pair.Key.ThreadState == ThreadState.Stopped) // || (pair.Key.ThreadState == ThreadState.Aborted)) // { // finishList.Add(pair.Key); // } //} foreach (HttpDownTask pair in downloadThreadMap) { //Thread downloadThread = pair.Key; //if ((pair.Key.ThreadState == ThreadState.Stopped) // || (pair.Key.ThreadState == ThreadState.Aborted)) if (pair.finished) { finishList.Add(pair); } } // 清理下载完成的任务 foreach (HttpDownTask finishThread in finishList) { downloadThreadMap.Remove(finishThread); } finishList.Clear(); // 当前线程处理已满 if (downloadThreadMap.Count >= maxThreadNum) { Thread.Sleep(100); continue; } HttpDownTask curTask = null; //Thread curThread; // 有空闲线程,则尝试开启下载任务 while ((GetHighPriorityQueueCount() > 0) && (downloadThreadMap.Count < maxThreadNum)) { curTask = DequeueHighPriorityTask(); if (null != curTask) { //LogSys.Info("taskid[{0}] Begin to down normal file[{1}]", curTask.taskId, curTask.downLoadUrl); ThreadPool.QueueUserWorkItem(new WaitCallback(curTask.DownLoadFile), 1); //curThread = new Thread(new ThreadStart(curTask.DownLoadFile)); //downloadThreadMap.Add(curThread, curTask); downloadThreadMap.Add(curTask); //curThread.Start(); } } while ((downloadThreadMap.Count < maxThreadNum) && (GetNormalQueueCount() > 0)) { curTask = DequeueNormalTask(); if (null != curTask) { //LogSys.Info("taskId[{0}] Begin to down high priority file[{1}]", curTask.taskId, curTask.downLoadUrl); ThreadPool.QueueUserWorkItem(new WaitCallback(curTask.DownLoadFile), 1); //curThread = new Thread(new ThreadStart(curTask.DownLoadFile)); downloadThreadMap.Add(curTask); //curThread.Start(); } } if (!IsBusy()) { // 已经空闲了 Thread.Sleep(100); } } }