TaskDownloadManager.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace HttpDownload
  7. {
  8. public class TaskDownloadManager
  9. {
  10. public int Capacity { get; set; }
  11. // DownloadTask list
  12. ArrayList allTasks = new ArrayList();
  13. LimitedConcurrencyLevelTaskScheduler lcts = null;
  14. TaskFactory factory = null;
  15. /// <summary>
  16. ///
  17. /// </summary>
  18. /// <param name="count">最大同时任务数[1, 30], 异常为5</param>
  19. public TaskDownloadManager(int count)
  20. {
  21. //ThreadPool.SetMaxThreads(count, count);
  22. if (count >= 1 && count <= 30)
  23. Capacity = count;
  24. else
  25. Capacity = 5;
  26. lcts = new LimitedConcurrencyLevelTaskScheduler(Capacity);
  27. factory = new TaskFactory(lcts);
  28. }
  29. /// <summary>
  30. /// 添加任务到队列
  31. /// </summary>
  32. /// <param name="fileUrl">文件的下载地址</param>
  33. /// <param name="fileDir">文件再本地的保存路径</param>
  34. /// <param name="offset">在文件中开始下载的的位置, byte</param>
  35. /// <returns>任务</returns>
  36. public DownloadTask enqueueTask(string fileUrl, string fileDir, string expectedMD5, long offset = 0L)
  37. {
  38. DownloadTask task = new DownloadTask(fileUrl, fileDir, expectedMD5, offset);
  39. allTasks.Add(task);
  40. return task;
  41. }
  42. public DownloadTask enqueueTask(string taskId, string fileUrl, string fileDir, string expectedMD5, long offset = 0L)
  43. {
  44. DownloadTask task = new DownloadTask(taskId, fileUrl, fileDir, expectedMD5, offset);
  45. allTasks.Add(task);
  46. return task;
  47. }
  48. public DownloadTask getDownloadTaskByDownloadTaskId(string taskId) {
  49. foreach (DownloadTask down in allTasks) {
  50. if (taskId == down.BelongingId) {
  51. return down;
  52. }
  53. }
  54. return null;
  55. }
  56. public void runAllTasks()
  57. {
  58. foreach (DownloadTask task in allTasks)
  59. {
  60. task.resumeTask();
  61. factory.StartNew(() => { DownloadTask.download(task); });
  62. }
  63. }
  64. public void pauseAllTasks()
  65. {
  66. foreach (DownloadTask task in allTasks)
  67. {
  68. task.pauseTask();
  69. }
  70. }
  71. public void resumeAllTasks()
  72. {
  73. foreach (DownloadTask task in allTasks)
  74. {
  75. task.resumeTask();
  76. factory.StartNew(() => { DownloadTask.download(task); });
  77. }
  78. }
  79. public ArrayList getAllTasks() { return allTasks; }
  80. public Hashtable getAllTasksMap() {
  81. Hashtable table = new Hashtable();
  82. foreach (DownloadTask down in allTasks) {
  83. string belongingid = down.BelongingId;
  84. if (table.ContainsKey(belongingid))
  85. {
  86. ArrayList arr = (ArrayList)table[belongingid];
  87. arr.Add(down);
  88. }
  89. else {
  90. ArrayList arr = new ArrayList();
  91. arr.Add(down);
  92. table.Add(belongingid, arr);
  93. }
  94. }
  95. return table;
  96. }
  97. public void runTask(DownloadTask task)
  98. {
  99. task.resumeTask();
  100. factory.StartNew(() => { DownloadTask.download(task); });
  101. }
  102. public void pauseTask(DownloadTask task)
  103. {
  104. task.pauseTask();
  105. }
  106. public void resumeTask(DownloadTask task)
  107. {
  108. task.resumeTask();
  109. factory.StartNew(() => { DownloadTask.download(task); });
  110. }
  111. public void removeTask(DownloadTask task)
  112. {
  113. task.pauseTask();
  114. allTasks.Remove(task);
  115. }
  116. }
  117. }