/// <summary> /// 任务状态变换 /// </summary> /// <param name="task"></param> private void OnStatusChanged(Aria2cTask task) { switch (task.Status) { case Aria2cTaskStatus.Active: OnUnPause?.Invoke(this, new Aria2cTaskEvent(task)); break; case Aria2cTaskStatus.Complete: { OnFinish?.Invoke(this, new Aria2cTaskEvent(task)); RemoveDownTask(task.Gid); if (CountDownTask() == 0) { OnAllFinish?.Invoke(this, new EventArgs()); } } break; case Aria2cTaskStatus.Error: OnError?.Invoke(this, new Aria2cTaskEvent(task)); break; case Aria2cTaskStatus.Paused: OnPaused?.Invoke(this, new Aria2cTaskEvent(task)); break; case Aria2cTaskStatus.Removed: OnRemoved?.Invoke(this, new Aria2cEvent(task.Gid)); downLoadDict.Remove(task.Gid); break; case Aria2cTaskStatus.Waiting: OnWaiting?.Invoke(this, new Aria2cTaskEvent(task)); break; default: break; } }
/// <summary> /// 全局状态 /// </summary> private async void GetGlobalStatus(Aria2cGlobalStat status) { try { if (status == null && isCycle) { status = this.GetGlobalStat(); } if (status == null) { return; } if (globalStat.DownloadSpeed != status.DownloadSpeed || globalStat.UploadSpeed != status.UploadSpeed || globalStat.NumActive != status.NumActive || globalStat.NumWaiting != status.NumWaiting || globalStat.NumStopped != status.NumStopped || globalStat.NumStoppedTotal != status.NumStoppedTotal) { OnGlobalStatusChanged?.Invoke(this, new Aria2cGlobalStatEvent(status)); globalStat = status; } if (status.NumActive == 0L && status.NumWaiting == 0L && status.DownloadSpeed == 0L && isCycle) { await Task.Delay(Convert.ToInt32(this.dInterval * 3)); //等待3秒后,再做判断,避免刚添加完任务的瞬间,误判定为全部结束 status = this.GetGlobalStat(); //获取最新status //timerTMP == null是一个flag,表明OnAllFinish没有触发过 if (status.NumActive == 0L && status.NumWaiting == 0L && status.DownloadSpeed == 0L && !IsLoading && timerTMP == null) { OnAllFinish?.Invoke(this, new Aria2cGlobalStatEvent(status)); StartTmpTimer(); } } } catch (Exception ex) { Console.WriteLine(ex); } }
/// <summary> /// 全局状态 /// </summary> private void GetGlobalStatus(Aria2cGlobalStat status) { try { if (status == null && !IsPauseAll) { status = this.GetGlobalStat(); } if (status == null) { return; } if (globalStat.DownloadSpeed != status.DownloadSpeed || globalStat.UploadSpeed != status.UploadSpeed || globalStat.NumActive != status.NumActive || globalStat.NumWaiting != status.NumWaiting || globalStat.NumStopped != status.NumStopped || globalStat.NumStoppedTotal != status.NumStoppedTotal) { OnGlobalStatusChanged?.Invoke(this, new Aria2cGlobalStatEvent(status)); globalStat = status; } if (status.NumActive == 0 && status.NumWaiting == 0 && status.DownloadSpeed == 0 && !IsPauseAll) { OnAllFinish?.Invoke(this, new Aria2cGlobalStatEvent(status)); if (timerTMP == null) { timerTMP = new Timer(10000);//10s后执行(等待其他如GlobalStatus),改变“暂停”变量,减少计时器轮询操作 timerTMP.Elapsed += new ElapsedEventHandler(SetPauseAll); timerTMP.AutoReset = false; timerTMP.Start(); } } } catch (Exception ex) { Console.WriteLine(ex); } }