异步下载总进度

时间:2014-01-03 12:10:40

标签: c# asynchronous download percentage

好吧,我创建了一个下载程序,用于检查列表中的所有文件,然后下载所有文件。

示例:

节点类:

public class Node
{
    public string fileName { get; set; }
    public string fileHash { get; set; }
    public int fileSize    { get; set; }

    public Node(string fName, string fHash, int fSize)
    {
        fileName = fName;
        fileHash = fHash;
        fileSize = fSize;
    }
}

public class Nodes : List<Node>
{
    public void Fill(string name, string hash, int size)
    {
        Add(new Node(name, hash, size));
    }
}

itemsUpdate:

List<Node> itemsUpdate = new List<Node>();

正在下载文件:

FileDownload Class

FileDownload fDownload;
foreach (Node n in itemsUpdate)
{
    //fDownload contains all for the async download.
    fDownload.Download(url, n.fileName);
    //Here I show the current progress
    while (fDownload.Progress != 100)
    {
        lblProgress.Text = fDownload.Progress.ToString() + "%";
    }
    lblProgress.Text = "100%";
}

我想知道的是如何计算下载的总进度。 希望有人可以帮助我。 感谢

2 个答案:

答案 0 :(得分:1)

首先计算所有文件大小的总和,然后每次计算总进度

long sum = 0;
foreach(Node n in itemsUpdate)
{
    sum += n.fileSize;
}

long nodeSum = 0;
foreach (Node n in itemsUpdate)
{
    //fDownload contains all for the async download.
    fDownload.Download(url, n.fileName);
    //Here I show the current progress
    while (fDownload.Progress != 100)
    {
        lblProgress.Text = ((nodeSum + fDownload.Progress * n.fileSize)/sum).ToString() + "%";
    }
    nodeSum += n.fileSize;
}

答案 1 :(得分:0)

  1. 您必须遍历所有文件队列才能获得其总大小there is a discussion about how it is made
  2. 然后,在下载callbacks时,必须检查进度,不仅要相对于当前文件大小,还要相对于所有文件大小总和