NEWS

[DEVEXPRESS] Tích hợp Pause/Resume vào download Progress Cell Gridview

[DEVEXPRESS] Tích hợp Pause/Resume vào download Progress Cell Gridview
Đăng bởi: Thảo Meo - Lượt xem: 5411 10:49:00, 22/02/2020DEVEXPRESS   In bài viết

Xin chào các bạn, hôm nay mình tiếp tục hướng dẫn các bạn download file tích hợp thêm chức năng pause và resume trên Gridview cell progress Devexpress.

[DEVEXPRESS] Download file with Pause/Resume Progressbar Cell C#

Trong bài viết trước, mình đã có viết hai bài về download.

  1. Bài download file trên Gridview cell với progress bar
  2. Và bài sử dụng thư viện AltoHttp

Trong bài này, mình sẽ tích hợp 2 chức năng này vào trong một app.

Giao diện ứng dụng:

pause_resume_demo

Source code C#:

using AltoHttp;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace progressbar_GridView
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            var listDownload = new List<FileDownload>();
            var fileURL = new FileDownload();
            fileURL.url = @"https://dl.teamviewer.com/download/version_15x/TeamViewer_Setup.exe";
            fileURL.progress = 0;
            fileURL.status = "Ready";
            fileURL.speed = "";
            fileURL.data = "";
            fileURL.filename = Path.GetFileName(fileURL.url);
            listDownload.Add(fileURL);
         

            var fileURL3 = new FileDownload();
            fileURL3.url = @"https://nodejs.org/dist/v13.9.0/node-v13.9.0-x64.msi";
            fileURL3.progress = 0;
            fileURL3.data = "";
            fileURL3.speed = "";
            fileURL3.status = "Ready";
            fileURL3.filename = Path.GetFileName(fileURL3.url);
            listDownload.Add(fileURL3);

            var fileURL4 = new FileDownload();
            fileURL4.url = @"https://download.microsoft.com/download/1/9/4/1949aa9c-6536-48f2-81fa-e7bb07410b36/SSMS-Setup-ENU.exe";
            fileURL4.progress = 0;
            fileURL4.data = "";
            fileURL4.speed = "";
            fileURL4.status = "Ready";
            fileURL4.filename = Path.GetFileName(fileURL4.url);
            listDownload.Add(fileURL4);
            gridControl1.DataSource = listDownload;
        }

        private void UpdateProgressByRowHandle(int rowHandle, string column, string message)
        {
            gridControl1.BeginInvoke((Action)(() =>
            {
                gridView1.SetRowCellValue(rowHandle, column, message);
            }));
        }
        Dictionary<int, HttpDownloader> listDownloader = new Dictionary<int, HttpDownloader>();
        private void gridView1_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            if (e.Column.Name.Equals("grid_pause"))
            {
                var mDownloader = listDownloader.FirstOrDefault(x => x.Key == e.RowHandle).Value;
                if(mDownloader != null)
                {
                    mDownloader.Pause();
                    UpdateProgressByRowHandle(e.RowHandle, "status", "Paused");

                }

            }
            if (e.Column.Name.Equals("grid_resume"))
            {
                var mDownloader = listDownloader.FirstOrDefault(x => x.Key == e.RowHandle).Value;
                if (mDownloader != null)
                {
                    mDownloader.ResumeAsync();
                    UpdateProgressByRowHandle(e.RowHandle, "status", "Resume...");
                }

            }
            if (e.Column.Name.Equals("grid_Action"))
            {
                var selectedRow = gridView1.GetSelectedRows();
                var urls = string.Join(",", from r in selectedRow where gridView1.IsDataRow(Convert.ToInt32(r)) select gridView1.GetRowCellValue(Convert.ToInt32(r), "url"));

                if (string.IsNullOrEmpty(urls))
                {
                    var url_one = gridView1.GetRowCellValue(e.RowHandle, "url").ToString();
                    UpdateProgressByRowHandle(Convert.ToInt16(e.RowHandle.ToString()), "status", "Initializing...");
                    DownloadFile(url_one, Application.StartupPath + @"download" + Path.GetFileName(url_one), e.RowHandle);
                    return;
                }



                var list_Rowhandles = string.Join(",", from r in selectedRow where gridView1.IsDataRow(Convert.ToInt32(r)) select gridView1.GetRowHandle(Convert.ToInt32(r)));

                var url = urls.Split(',');
                var list_Rowhandle = list_Rowhandles.Split(',');
                for (int i = 0; i < url.Length; i++)
                {

                    UpdateProgressByRowHandle(Convert.ToInt16(list_Rowhandle[i].ToString()), "status", "Initializing...");
                    DownloadFile(url[i].ToString(), Application.StartupPath + @"download" + Path.GetFileName(url[i].ToString()), Convert.ToInt16(list_Rowhandle[i].ToString()));

                }



            }
        }


        private void downloadComplete(object sender, EventArgs e, int rowHandle)
        {
            listDownloader.Remove(rowHandle);
            UpdateProgressByRowHandle(rowHandle, "status", "Finish.");
        }

        private void downloadProgress(object sender, AltoHttp.DownloadProgressChangedEventArgs e, int rowHandle, HttpDownloader mDownloader)
        {
            UpdateProgressByRowHandle(rowHandle, "progress", e.Progress.ToString());
            UpdateProgressByRowHandle(rowHandle, "data", string.Format("{0} / {1} MB",
               (mDownloader.BytesReceived / 1024d / 1024d).ToString("0.00"),
               (mDownloader.ContentSize / 1024d / 1024d).ToString("0.00")));
          
            UpdateProgressByRowHandle(rowHandle, "status", "Downloading...");
            UpdateProgressByRowHandle(rowHandle, "speed", string.Format("{0} Kb/s", (e.Speed / 1024d).ToString("0.00")));


        }
        public void DownloadFile(string urlAddress, string location, int rowHandle)
        {

            var mDownloader = new HttpDownloader(urlAddress, location);
            try
            {
                listDownloader.Add(rowHandle, mDownloader);
            }
            catch (Exception)
            {
                UpdateProgressByRowHandle(rowHandle, "status", "File is Downloading...");
                //XtraMessageBox.Show("File is Downloading..", "Infomation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
         
            mDownloader.DownloadProgressChanged += new AltoHttp.ProgressChangedEventHandler((s, e) => downloadProgress(s,e, rowHandle, mDownloader));
            mDownloader.DownloadCompleted += new EventHandler((s,e) => downloadComplete(s,e, rowHandle));            


            mDownloader.StartAsync();
           
        }

        private void btn_resumeall_Click(object sender, EventArgs e)
        {
            foreach (var item in listDownloader.Values)
            {
                item.ResumeAsync();
            }
            foreach (var rowHandle in listDownloader.Keys)
            {
             
                UpdateProgressByRowHandle(rowHandle, "status", "Resume...");
            }
        }

        private void btn_pauseall_Click(object sender, EventArgs e)
        {
            foreach(var item in listDownloader.Values)
            {
                item.Pause();
            }
            foreach (var rowHandle in listDownloader.Keys)
            {

                UpdateProgressByRowHandle(rowHandle, "status", "Paused");
            }
        }
    }
    public class FileDownload
    {
        public string url { get; set; }
        public int progress { get; set; }
        public string speed { get; set; }
        public string status { get; set; }
        public string filename { get; set; }
        public string data { get; set; }

    }
}

Video Demo:

Thanks for watching!

 

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[DEVEXPRESS] Tích hợp Pause/Resume vào download Progress Cell Gridview
Đăng bởi: Thảo Meo - Lượt xem: 5411 10:49:00, 22/02/2020DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.