Exemple #1
0
        /// <summary>
        /// 刷新下载状态
        /// </summary>
        private void RefreshList()
        {
            TotalProgressChanged();

            //根据numOnce及正在下载的情况生成下载
            int downloadingCount = webs.Count;

            for (int j = 0; j < NumOnce - downloadingCount; j++)
            {
                if (numLeft > 0)
                {
                    DownloadItem dlitem = downloadItems[downloadItems.Count - numLeft];

                    string url  = dlitem.Url;
                    string file = dlitem.FileName.Replace("\r\n", "");
                    string path = GetLocalPath(dlitem);

                    //检查目录长度
                    if (path.Length > 248)
                    {
                        downloadItems[downloadItems.Count - numLeft].StatusE = DLStatus.Failed;
                        downloadItems[downloadItems.Count - numLeft].Size    = "路径太长";
                        WriteErrText(url + ": 路径太长");
                        j--;
                    }
                    else
                    {
                        dlitem.LocalFileName = ReplaceInvalidPathChars(file, path, 0);
                        file = dlitem.LocalName = path + dlitem.LocalFileName;

                        //检查全路径长度
                        if (file.Length > 258)
                        {
                            downloadItems[downloadItems.Count - numLeft].StatusE = DLStatus.Failed;
                            downloadItems[downloadItems.Count - numLeft].Size    = "路径太长";
                            WriteErrText(url + ": 路径太长");
                            j--;
                        }
                    }

                    if (File.Exists(file))
                    {
                        downloadItems[downloadItems.Count - numLeft].StatusE = DLStatus.IsHave;
                        downloadItems[downloadItems.Count - numLeft].Size    = "已存在跳过";
                        j--;
                    }
                    else if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    else
                    {
                        downloadItems[downloadItems.Count - numLeft].StatusE = DLStatus.DLing;

                        DownloadTask task = new DownloadTask(url, file, MainWindow.IsNeedReferer(url), dlitem.NoVerify);
                        webs.Add(url, task);

                        //异步下载开始
                        System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Download));
                        thread.Start(task);
                    }

                    numLeft--;
                }
                else
                {
                    break;
                }
            }
            RefreshStatus();
        }
Exemple #2
0
        //============================== Menu Function ===================================
        private void ExecuteDownloadListTask(DLWorkMode dlworkmode)
        {
            int selectcs, delitemfile = 0;
            List <DownloadItem> selected = new List <DownloadItem>();

            if (dlworkmode == DLWorkMode.RetryAll || dlworkmode == DLWorkMode.StopAll || dlworkmode == DLWorkMode.RemoveAll)
            {
                foreach (object o in dlList.Items)
                {
                    //转存集合,防止selected改变
                    DownloadItem item = (DownloadItem)o;
                    selected.Add(item);
                }
            }
            else
            {
                foreach (object o in dlList.SelectedItems)
                {
                    DownloadItem item = (DownloadItem)o;
                    selected.Add(item);
                }
            }
            selectcs = selected.Count;

            string        lpath = "";
            DirectoryInfo di;

            foreach (DownloadItem item in selected)
            {
                switch (dlworkmode)
                {
                case DLWorkMode.Retry:
                case DLWorkMode.RetryAll:
                    if (item.StatusE == DLStatus.Failed || item.StatusE == DLStatus.Cancel || item.StatusE == DLStatus.IsHave)
                    {
                        numLeft = numLeft > selectcs ? selectcs : numLeft;
                        downloadItems.Remove(item);
                        downloadItemsDic.Remove(item.Url);
                        AddDownload(new MiniDownloadItem[] {
                            new MiniDownloadItem(item.FileName, item.Url, item.Host, item.Author, item.LocalName, item.LocalFileName,
                                                 item.Id, item.NoVerify)
                        });
                    }
                    break;

                case DLWorkMode.Stop:
                case DLWorkMode.StopAll:
                    if (item.StatusE == DLStatus.DLing || item.StatusE == DLStatus.Wait)
                    {
                        if (webs.ContainsKey(item.Url))
                        {
                            webs[item.Url].IsStop = true;
                            webs.Remove(item.Url);
                        }
                        else
                        {
                            numLeft--;
                        }

                        if (dlworkmode == DLWorkMode.StopAll)
                        {
                            numLeft = 0;
                        }
                        item.StatusE = DLStatus.Cancel;
                        item.Size    = "已取消";

                        try
                        {
                            File.Delete(item.LocalFileName + DLEXT);

                            lpath = GetLocalPath(item);
                            di    = new DirectoryInfo(lpath);
                            if (di.GetFiles().Length + di.GetDirectories().Length < 1)
                            {
                                Directory.Delete(lpath);
                            }
                        }
                        catch { }
                    }
                    break;

                case DLWorkMode.Del:
                case DLWorkMode.Remove:
                case DLWorkMode.RemoveAll:
                    if (dlworkmode == DLWorkMode.Del && delitemfile < 1)
                    {
                        if (MessageBox.Show("QwQ 真的要把任务和文件一起删除么?",
                                            MainWindow.ProgramName,
                                            MessageBoxButton.YesNo,
                                            MessageBoxImage.Warning) == MessageBoxResult.No)
                        {
                            delitemfile = 2;
                        }
                        else
                        {
                            delitemfile = 1;
                        }
                    }
                    if (delitemfile > 1)
                    {
                        break;
                    }

                    if (item.StatusE == DLStatus.DLing)
                    {
                        if (webs.ContainsKey(item.Url))
                        {
                            webs[item.Url].IsStop = true;
                            webs.Remove(item.Url);
                        }
                    }
                    else if (item.StatusE == DLStatus.Success || item.StatusE == DLStatus.IsHave)
                    {
                        numSaved = numSaved > 0 ? --numSaved : 0;
                    }
                    else if (item.StatusE == DLStatus.Wait || item.StatusE == DLStatus.Cancel)
                    {
                        numLeft = numLeft > 0 ? --numLeft : 0;
                    }

                    downloadItems.Remove(item);
                    downloadItemsDic.Remove(item.Url);

                    //删除文件
                    string fname = item.LocalName;
                    if (dlworkmode == DLWorkMode.Del)
                    {
                        if (File.Exists(fname))
                        {
                            File.Delete(fname);
                        }

                        lpath = GetLocalPath(item);
                        di    = new DirectoryInfo(lpath);
                        if (di.GetFiles().Length + di.GetDirectories().Length < 1)
                        {
                            Directory.Delete(lpath);
                        }
                    }
                    break;
                }
            }
            if (dlworkmode == DLWorkMode.Stop || dlworkmode == DLWorkMode.Remove)
            {
                RefreshList();
            }
            if (dlworkmode == DLWorkMode.Remove)
            {
                RefreshStatus();
            }
        }
Exemple #3
0
        /// <summary>
        /// 下载,另一线程
        /// </summary>
        /// <param name="o"></param>
        private void Download(object o)
        {
            DownloadTask  task = (DownloadTask)o;
            FileStream    fs   = null;
            Stream        str  = null;
            SessionClient sc   = new SessionClient();

            System.Net.WebResponse res = null;
            double       downed        = 0;
            DownloadItem item          = downloadItemsDic[task.Url];

            try
            {
                res = sc.GetWebResponse(
                    task.Url,
                    MainWindow.WebProxy,
                    task.NeedReferer
                    );

                /////////开始写入文件
                str = res.GetResponseStream();
                byte[] bytes = new byte[5120];
                fs = new FileStream(task.SaveLocation + DLEXT, FileMode.Create);

                int      bytesReceived = 0;
                DateTime last          = DateTime.Now;
                int      osize         = str.Read(bytes, 0, bytes.Length);
                downed = osize;
                while (!task.IsStop && osize > 0)
                {
                    fs.Write(bytes, 0, osize);
                    bytesReceived += osize;
                    DateTime now   = DateTime.Now;
                    double   speed = -1;
                    if ((now - last).TotalSeconds > 0.6)
                    {
                        speed  = downed / (now - last).TotalSeconds / 1024.0;
                        downed = 0;
                        last   = now;
                    }
                    Dispatcher.Invoke(new DownloadHandler(web_DownloadProgressChanged),
                                      res.ContentLength, bytesReceived / (double)res.ContentLength * 100.0, task.Url, speed);
                    osize   = str.Read(bytes, 0, bytes.Length);
                    downed += osize;
                }
            }
            catch (Exception ex)
            {
                //Dispatcher.Invoke(new UIdelegate(delegate(object sender) { StopLoadImg(re.Key, re.Value); }), "");
                task.IsStop = true;
                Dispatcher.Invoke(new VoidDel(delegate()
                {
                    //下载失败
                    if (downloadItemsDic.ContainsKey(task.Url))
                    {
                        item.StatusE = DLStatus.Failed;
                        item.Size    = "下载失败";
                        WriteErrText(task.Url);
                        WriteErrText(task.SaveLocation);
                        WriteErrText(ex.Message + "\r\n");

                        try
                        {
                            if (fs != null)
                            {
                                fs.Close();
                            }
                            if (str != null)
                            {
                                str.Close();
                            }
                            if (res != null)
                            {
                                res.Close();
                            }

                            File.Delete(task.SaveLocation + DLEXT);
                            DelDLItemNullDirector(item);
                        }
                        catch { }
                    }
                }));
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
                if (str != null)
                {
                    str.Close();
                }
                if (res != null)
                {
                    res.Close();
                }
            }

            if (task.IsStop)
            {
                //任务被取消
                Dispatcher.Invoke(new VoidDel(delegate()
                {
                    if (downloadItemsDic.ContainsKey(task.Url))
                    {
                        if (!dlerrtxt.Contains(item.Size))
                        {
                            item.StatusE = DLStatus.Cancel;
                        }
                    }
                }));

                try
                {
                    File.Delete(task.SaveLocation + DLEXT);
                    DelDLItemNullDirector(item);
                }
                catch { }
            }
            else
            {
                //下载成功完成
                Dispatcher.Invoke(new VoidDel(delegate()
                {
                    try
                    {
                        //DownloadTask task1 = obj as DownloadTask;

                        //判断完整性
                        if (!item.NoVerify && 100 - item.Progress > 0.001)
                        {
                            task.IsStop  = true;
                            item.StatusE = DLStatus.Failed;
                            item.Size    = "下载未完成";
                            try
                            {
                                File.Delete(task.SaveLocation + DLEXT);
                                DelDLItemNullDirector(item);
                            }
                            catch { }
                        }
                        else
                        {
                            //修改后缀名
                            File.Move(task.SaveLocation + DLEXT, task.SaveLocation);

                            item.Progress = 100.0;
                            item.StatusE  = DLStatus.Success;
                            //downloadItemsDic[task.Url].Size = (downed > 1048576
                            //? (downed / 1048576.0).ToString("0.00MB")
                            //: (downed / 1024.0).ToString("0.00KB"));
                            numSaved++;
                        }
                    }
                    catch { }
                }));
            }

            //下载结束
            Dispatcher.Invoke(new VoidDel(delegate()
            {
                webs.Remove(task.Url);
                RefreshList();
            }));
        }