/// <summary> /// 停止下载某个name文件 /// </summary> /// <param name="fileName"></param> public static void StopDownloadFile(string fileName) { Debug.LogError(m_CurrentDownload.FileName); if (m_CurrentDownload != null && m_CurrentDownload.Key == fileName) { m_Downloader.CancelAsync(); } else { lock (m_LockObject) { DownloadCacheInfo cacheInfo = m_WaitingDownloadList.Find((temp) => fileName == temp.Key); m_WaitingDownloadList.Remove(cacheInfo); if (cacheInfo.ComplatedCallBack != null) { try { cacheInfo.ComplatedCallBack(cacheInfo.Key, new AsyncCompletedEventArgs(null, true, null)); } catch (Exception ex) { Debug.LogException(ex); } } } } }
/// <summary> /// 停止下载 /// </summary> public static void StopDownloader() { lock (m_LockObject) { for (int i = 0; i < m_WaitingDownloadList.Count; i++) { DownloadCacheInfo cacheInfo = m_WaitingDownloadList[i]; if (cacheInfo.ComplatedCallBack != null) { try { m_CurrentDownload.ComplatedCallBack(m_CurrentDownload.Key, new AsyncCompletedEventArgs(null, true, null)); } catch (Exception ex) { Debug.LogException(ex); } } } m_WaitingDownloadList.Clear(); } if (m_Downloader != null) { m_Downloader.CancelAsync(); } }
/// <summary> /// 尝试下载文件 /// </summary> static void TryStartDownloadOneFile() { if (m_CurrentDownload == null) { if (m_WaitingDownloadList.Count > 0) { lock (m_LockObject) { m_CurrentDownload = m_WaitingDownloadList[0]; m_WaitingDownloadList.RemoveAt(0); } if (m_Downloader == null) { m_Downloader = new WebClient(); m_Downloader.DownloadFileCompleted += DownloadFile_Complated; m_Downloader.DownloadProgressChanged += DownloadFile_ProgressChanged; } m_Downloader.DownloadFileAsync(new Uri(m_CurrentDownload.RemoteFileFullName), m_CurrentDownload.CacheFileFullName); } else { ReleaseDownLoader(); } } }
/// <summary> /// 添加到下载队列中 /// </summary> /// <param name="fileName">文件名称</param> /// <param name="complated">完成通知</param> /// <param name="progressChanged">进度更新</param> /// <returns></returns> public static void AppendDownloadFile(string fileName, Action <string, AsyncCompletedEventArgs> complated = null, Action <string, DownloadProgressChangedEventArgs> progressChanged = null) { DownloadCacheInfo cacheInfo = null; if (m_CurrentDownload != null && m_CurrentDownload.Key == fileName) { cacheInfo = m_CurrentDownload; } else { m_WaitingDownloadList.Find(temp => temp.Key == fileName); } if (cacheInfo == null) { cacheInfo = new DownloadCacheInfo(fileName, fileName); m_WaitingDownloadList.Add(cacheInfo); } if (complated != null) { if (cacheInfo.ComplatedCallBack != null) { cacheInfo.ComplatedCallBack += complated; } else { cacheInfo.ComplatedCallBack = complated; } } if (progressChanged != null) { if (cacheInfo.ProgressChanged != null) { cacheInfo.ProgressChanged += progressChanged; } else { cacheInfo.ProgressChanged = progressChanged; } } TryStartDownloadOneFile(); }
/// <summary> /// 下载文件完成通知 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> static void DownloadFile_Complated(object sender, AsyncCompletedEventArgs e) { try { if (m_CurrentDownload != null && m_CurrentDownload.ComplatedCallBack != null) { m_CurrentDownload.ComplatedCallBack(m_CurrentDownload.Key, e); } } catch (Exception ex) { Debug.LogException(ex); } finally { m_CurrentDownload = null; TryStartDownloadOneFile(); } }