코드가 작동 중입니다. 그러나 이제는 각 파일의 다운로드 진행률을 progressBar1
에 표시하고 있습니다. 하지만 디자이너 (이미 추가됨)에 전체 다운로드 진행 상태를 표시하려면 progressBar2
을 추가하고 싶습니다. 어떻게 계산하여 progressBar2
에 표시 할 수 있습니까?전체 다운로드를 표시하는 새로운 progressBar를 어떻게 추가 할 수 있습니까?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Threading;
using System.Diagnostics;
namespace DownloadFiles
{
public partial class Form1 : Form
{
Stopwatch sw = new Stopwatch();
int count = 0;
PictureBoxBigSize pbbs;
ExtractImages ei = new ExtractImages();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnDownload_Click(object sender, EventArgs e)
{
downloadFile(filesUrls);
}
private Queue<string> _downloadUrls = new Queue<string>();
private async void downloadFile(IEnumerable<string> urls)
{
foreach (var url in urls)
{
_downloadUrls.Enqueue(url);
}
await DownloadFile();
}
private async Task DownloadFile()
{
if (_downloadUrls.Any())
{
WebClient client = new WebClient();
client.DownloadProgressChanged += ProgressChanged;
client.DownloadFileCompleted += Completed;
var url = _downloadUrls.Dequeue();
await client.DownloadFileTaskAsync(new Uri(url), @"C:\Temp\DownloadFiles\" + count + ".jpg");
return;
}
}
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
// Calculate download speed and output it to labelSpeed.
Label2.Text = string.Format("{0} kb/s", (e.BytesReceived/1024d/sw.Elapsed.TotalSeconds).ToString("0.00"));
// Update the progressbar percentage only when the value is not the same.
double bytesIn = double.Parse(e.BytesReceived.ToString());
double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
double percentage = bytesIn/totalBytes * 100;
ProgressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());//e.ProgressPercentage;
// Show the percentage on our label.
Label4.Text = e.ProgressPercentage.ToString() + "%";
// Update the label with how much data have been downloaded so far and the total size of the file we are currently downloading
Label5.Text = string.Format("{0} MB's/{1} MB's",
(e.BytesReceived/1024d/1024d).ToString("0.00"),
(e.TotalBytesToReceive/1024d/1024d).ToString("0.00"));
}
// The event that will trigger when the WebClient is completed
private async void Completed(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled == true)
{
MessageBox.Show("Download has been canceled.");
}
else
{
ProgressBar1.Value = 100;
count++;
await DownloadFile();
}
}
}
}
다운로드 할 때마다 다운로드 한 총 바이트 수를 저장하는 int를 만듭니다 (int bytesCompleted). 별도의 방법으로 다운로드하려는 모든 파일을 반복하여 해당 번호를 별도의 int (int totalBytesAllDownloads)에 저장합니다. progressChanged에서 ProgressBar2 ((e.BytesReceived + bytesCompleted)/totalBytesDownloaded)로 다시보고하십시오. 나는 그것이 그것보다 더 이상 복잡해야한다고 생각하지 않는다! –
@Shn_Android_Dev 코드로 수행하는 방법을 보여 주시겠습니까? 나는 분리 된 방법과 나머지를 이해하지 못했다. 죄송합니다. –