/// <summary> /// 开始任务 /// </summary> public void StartData(DwonloadEntity urlList) { tokenSource = new CancellationTokenSource(); resetEvent = new ManualResetEvent(true); thread = new Thread(new ThreadStart(() => StartTask(urlList))); thread.Start(); }
/// <summary> /// 执行数据 /// </summary> /// <param name="Index"></param> public void Execute(DwonloadEntity Index) { //阻止当前线程 resetEvent.WaitOne(); Console.WriteLine("当前第" + Index + "个线程"); Thread.Sleep(1000); }
/// <summary> /// 初始化异步队列 /// </summary> private void InitAsyncQueue(DwonloadEntity item) { MeterInfo info = new MeterInfo(); info.entity = item; DownloadFile(item); AsyncQueues.Enqueue(new MeterAsyncQueue() { MeterInfoTask = info }); }
//控制线程并行数量 public void StartTask(DwonloadEntity Ids) { IsInitTask = true; ParallelTasks = new List <Task>(); AsyncQueues = new Queue <MeterAsyncQueue>(); //获取并发数 ParallelTaskCount = 5; //初始化异步队列 InitAsyncQueue(Ids); //开始执行队列任务 HandlingTask(); Task.WaitAll(new Task[] { Task.WhenAll(ParallelTasks.ToArray()) }); }