- [DEVEXPRESS] Chia sẻ code các tạo report in nhiều hóa đơn trên XtraReport C#
- [POWER AUTOMATE] Hướng dẫn gởi tin nhắn zalo từ file Excel - No code
- [C#] Chia sẻ code lock và unlock user trong domain Window
- [SOFTWARE] Giới thiệu bộ phần mềm tính Kết Cấu Thép HatteSale, Mộng Đơn, Dầm, Sàn, Móng Cọc, Vách, Xà Gồ, Tính Tải Trọng
- [DEVEXPRESS] Vẽ Biểu Đồ Stock Chứng Khoán - Công Cụ Thiết Yếu Cho Nhà Đầu Tư trên Winform
- [C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform
- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
[DEVEXPRESS] Tích hợp Pause/Resume vào download Progress Cell Gridview
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.
- Bài download file trên Gridview cell với progress bar
- 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:
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!