Derezzed
Derezzed

Reputation: 1113

Asynchronous Download total progress

Well, I created a downloader that checks all files in a list and then it downloads all files.

Example:

Node Class:

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>();

Downloading the files:

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%";
}

What I want to know is how to calculate the total progress of the download. Hope someone could help me. Thanks

Upvotes: 0

Views: 154

Answers (2)

RyanWang
RyanWang

Reputation: 178

first calculate sum of all file size, then calculate total progress each time

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;
}

Upvotes: 1

Illia Tereshchuk
Illia Tereshchuk

Reputation: 1202

  1. You have to loop over all the files queue in order to acquire their total size, and there is a discussion about how it is made.
  2. Then, in download callbacks progress must be checked not only relatively to current file size, but relatively to all files sizes sum

Upvotes: 0

Related Questions