winforms - 如何在 WinForms 中制作文件下载进度条?
问题描述
我正在用 CefSharp 制作一个网络浏览器。我在 2 天前刚刚实现了下载,但下载没有进度条。如何让进度条显示下载进度?
解决方案
编辑:让事情更清楚
将 ProgressBar 控件添加到您的表单,并在它旁边添加一个 BackgroundWorker 组件到您的表单。首先弄清楚你的文件大小:
Int64 bytes_total= Convert.ToInt64(client.ResponseHeaders["Content-Length"])
这是来自 Alex 的惊人答案的代码,可以在这里找到:
public Form1()
{
InitializeComponent();
backgroundWorker1.DoWork += backgroundWorker1_DoWork;
backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
backgroundWorker1.WorkerReportsProgress = true;
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
//Replace this code with a way to figure out how much of the file you have already downloaded and then use backgroundworker1.ReportProgress and send the percentage completion of the file download.
for (int i = 0; i < 100; i++)
{
Thread.Sleep(1000);
backgroundWorker1.ReportProgress(i);
}
}
private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
推荐阅读
- c# - Xamarin Forms iOS 应用程序在启动时突然崩溃
- php - 谷歌云存储 500 内部服务器错误
- python - 将 .tsv 文件转换为 .txt 会创建意外字符,可能修复吗?
- symfony - PHP 致命错误:未捕获的 RuntimeException:未定义 APP_ENV 环境变量
- javascript - 使用来自输入的数据更新数组
- git - 将 VSTS / Azure DevOps 中所有提交详细信息的列表导出到文件中?
- kubernetes - 无法在 minikube 上部署 debezium
- sql-server - 将 DD-MON-YYYY AM/PM 转换或转换为 YYYY-MM-DD
- webview - WKWebView 在 Safari 中打开选项卡而不是在 WebView 中
- postgresql - brew 服务启动 postgresql 不工作