public void AnalysisTask(TaskInfo taskInfo)
        {
            var t = new Thread(() =>
            {
                try
                {
                    taskInfo.Status = DownloadStatus.TaskAnalysis;
                    PreDelegates.Refresh(new ParaRefresh(taskInfo));

                    if (taskInfo.Analysis())
                    {
                        taskInfo.Status = DownloadStatus.AnalysisComplete;
                        PreDelegates.Refresh(new ParaRefresh(taskInfo));
                    }
                    else
                    {
                        taskInfo.Status = DownloadStatus.AnalysisFailed;
                        PreDelegates.Refresh(new ParaRefresh(taskInfo));
                    }

                }
                catch (Exception ex)
                {
                    CoreManager.LogManager.Debug(ex.ToString());
                    taskInfo.Status = DownloadStatus.Error;
                    PreDelegates.Refresh(new ParaRefresh(taskInfo));
                    CoreManager.LogManager.Debug(ex.ToString());
                }

            }) {IsBackground = true};
            t.Start();
        }
        /// <summary>
        /// 增加任務
        /// </summary>
        /// <param name="plugin">任務的插件</param>
        /// <param name="url">任務網址</param>
        /// <returns></returns>
        public TaskInfo AddTask(IPlugin plugin, string url)
        {
            //建立TaskInfo物件
            var taskInfo = new TaskInfo
                {
                    SaveDirectoryName = CoreManager.ConfigManager.Settings.DefaultSaveFolder,
                    Url = url,
                    TextEncoding = System.Text.Encoding.GetEncoding(CoreManager.ConfigManager.Settings.TextEncoding),
                    Status = DownloadStatus.TaskAnalysis
                };
            taskInfo.SetPlugin(plugin);

            //向任務集合增加新任務
            Monitor.Enter(TaskInfosLock);
            TaskInfos.Add(taskInfo);
            Monitor.Exit(TaskInfosLock);
            //重新整理UI
            if (PreDelegates.Refresh != null)
                PreDelegates.Refresh(new ParaRefresh(taskInfo));
            return taskInfo;
        }
        public void AnalysisTask(TaskInfo taskInfo)
        {
            var t = new Thread(() =>
            {
                try
                {
                    taskInfo.Status = DownloadStatus.TaskAnalysis;
                    PreDelegates.Refresh(new ParaRefresh(taskInfo));

                    if (taskInfo.Analysis())
                    {
                        taskInfo.Status = DownloadStatus.AnalysisComplete;
                        if (taskInfo.CustomFileName == "Unknow")
                        {
                            FormatFileName FFN = new FormatFileName();
                            taskInfo.CustomFileName = FFN.OutputFormat(taskInfo, CoreManager.ConfigManager.Settings.SelectFormat);
                        }
                        PreDelegates.Refresh(new ParaRefresh(taskInfo));
                    }
                    else
                    {
                        taskInfo.Status = DownloadStatus.AnalysisFailed;
                        PreDelegates.Refresh(new ParaRefresh(taskInfo));
                    }

                }
                catch (Exception ex)
                {
                    CoreManager.LogManager.Debug(ex.ToString());
                    taskInfo.Status = DownloadStatus.Error;
                    PreDelegates.Refresh(new ParaRefresh(taskInfo));
                    CoreManager.LogManager.Debug(ex.ToString());
                }

            }) {IsBackground = true};
            t.Start();
        }
        /// <summary>
        /// 切換訂閱任務
        /// </summary>
        /// <param name="taskInfo"></param>
        public void SwitchSubscribe(TaskInfo taskInfo)
        {
            var t = new Thread(() =>
            {
                try
                {
                    taskInfo.Subscribe = !taskInfo.Subscribe;
                }
                catch (Exception ex)
                {
                    CoreManager.LogManager.Debug(ex.ToString());
                    taskInfo.Status = DownloadStatus.Error;

                }
                //重新整理UI
                PreDelegates.Refresh(new ParaRefresh(taskInfo));

            }) { IsBackground = true };

            t.Start();
        }
        /// <summary>
        /// 停止任務
        /// </summary>
        /// <param name="taskInfo"></param>
        public void StopTask(TaskInfo taskInfo)
        {
            //只有已開始的任務才能停止
            switch (taskInfo.Status)
            {

                case DownloadStatus.Downloading:
                    taskInfo.Status = DownloadStatus.Stopping;

                    break;
                default:
                    taskInfo.Status = DownloadStatus.TaskPause;
                    return;
            }

            //重新整理UI
            PreDelegates.Refresh(new ParaRefresh(taskInfo));
            //停止任務
            taskInfo.Stop();

            if (taskInfo.Status != DownloadStatus.TaskPause)
            {
                //啟動新執行緒等待任務完全停止
                var t = new Thread(() =>
                    {
                        //等待時間 (10秒)
                        int timeout = 10000;
                        //等待停止
                        while (taskInfo.Status == DownloadStatus.Stopping)
                        {
                            Thread.Sleep(500);
                            timeout -= 500;
                            if (timeout < 0 || taskInfo.HasStopped) //如果到时仍未停止
                            {
                                break;
                            }
                        }

                        taskInfo.Status = DownloadStatus.TaskPause;
                        //重新整理UI
                        PreDelegates.Refresh(new ParaRefresh(taskInfo));
                    }) {IsBackground = true};
                t.Start();
            }
            //釋放Downloader
            taskInfo.DisposeDownloader();
        }
        /// <summary>
        /// 刪除任任務(自動停止進行中的任務)
        /// </summary>
        /// <param name="taskInfo"></param>
        public void DeleteTask(TaskInfo taskInfo)
        {
            //停止任務
            StopTask(taskInfo);

            //啟動新執行緒等待任務完全停止
            ThreadPool.QueueUserWorkItem(o =>
                {
                    try
                    {
                        while (taskInfo.Status == DownloadStatus.Stopping || taskInfo.Status == DownloadStatus.Downloading)
                        {
                            Thread.Sleep(50);
                        }
                        Monitor.Enter(TaskInfosLock);
                        TaskInfos.Remove(taskInfo);
                        Monitor.Exit(TaskInfosLock);
                        //重新整理UI
                        PreDelegates.Refresh(new ParaRefresh(taskInfo));
                    }
                    catch (Exception ex)
                    {
                        CoreManager.LogManager.Debug(ex.ToString());
                        taskInfo.Status = DownloadStatus.Error;
                        PreDelegates.Refresh(new ParaRefresh(taskInfo));
                    }
                });
        }
Beispiel #7
0
 private void UpdateListViewItem(ListViewItem lvi,TaskInfo taskInfo)
 {
     lvi.SubItems[(int)LsvColumn.Subscribe].Text = taskInfo.GetSubscribe();
     lvi.SubItems[(int)LsvColumn.Status].Text = taskInfo.GetDownloadStatus();
     lvi.SubItems[(int)LsvColumn.Title].Text = taskInfo.Title.ToString(CultureInfo.InvariantCulture);
     lvi.SubItems[(int)LsvColumn.Progress].Text = string.Format(@"{0:P}", taskInfo.GetProgress());
     lvi.SubItems[(int)LsvColumn.TotalSection].Text = taskInfo.TotalSection.ToString(CultureInfo.InvariantCulture);
     lvi.SubItems[(int)LsvColumn.EndSection].Text = taskInfo.EndSection.ToString(CultureInfo.InvariantCulture);
     lvi.SubItems[(int)LsvColumn.CurrentSection].Text = taskInfo.CurrentSection.ToString(CultureInfo.InvariantCulture);
     lvi.SubItems[(int)LsvColumn.Author].Text = taskInfo.Author.ToString(CultureInfo.InvariantCulture);
     lvi.SubItems[(int)LsvColumn.FailTimes].Text = taskInfo.FailTimes.ToString(CultureInfo.InvariantCulture);
 }
Beispiel #8
0
        private void lsv_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!splitContainer1.IsCollpased) splitContainer1.CollpaseOrExpand();
            var lv =(ListView) sender;
            DisableExtraOptions();
            ClearExtraOptions();
            if (lv.SelectedItems.Count > 0)
            {
                _selectedTaskInfo = null;

                if (lv.SelectedItems.Count == 1)
                {
                    if (splitContainer1.IsCollpased) splitContainer1.CollpaseOrExpand();
                    ListViewItem sItem = lsv.SelectedItems[0];
                    TaskInfo taskInfo = GetTask(new Guid((string)sItem.Tag));
                    txtBeginSection.Text = taskInfo.BeginSection.ToString(CultureInfo.InvariantCulture);
                    txtEndSection.Text = taskInfo.EndSection.ToString(CultureInfo.InvariantCulture);
                    txtTitle.Text = taskInfo.CustomFileName.ToString(CultureInfo.InvariantCulture);
                    cbSaveDir.Text = taskInfo.SaveDirectoryName.ToString(CultureInfo.InvariantCulture);
                    _selectedTaskInfo = taskInfo;
                    if (taskInfo.Status!=DownloadStatus.Downloading)
                    {
                        EnabledExtraOptions();
                    }

                }
            }
        }