Download C#使用webclient进行多次下载

Download C#使用webclient进行多次下载,download,Download,我有一个下载列表到一个txt文件。() 它会检查crc32是否已更改,然后才将文件下载到客户端,问题是列表增长时花费的时间太长。 下载类: class FileDownloader { private static int curFile; private static long lastBytes; private static long currentBytes; private static Stopwatch stopWatch = new Stopw

我有一个下载列表到一个txt文件。() 它会检查crc32是否已更改,然后才将文件下载到客户端,问题是列表增长时花费的时间太长。 下载类:

 class FileDownloader
{
    private static int curFile;
    private static long lastBytes;
    private static long currentBytes;

    private static Stopwatch stopWatch = new Stopwatch();

    public static void DownloadFile()
    {
        if (Globals.OldFiles.Count <= 0)
        {
            Common.ChangeStatus("CHECKCOMPLETE");
            Common.UpdateCompleteProgress(100);
            Common.StartGame();
            return;
        }

        if (curFile >= Globals.OldFiles.Count)
        {
            Common.ChangeStatus("DOWNLOADCOMPLETE");
            Common.StartGame();
            return;
        }

        if (Globals.OldFiles[curFile].Contains("/"))
        {
            Directory.CreateDirectory(Path.GetDirectoryName(Globals.OldFiles[curFile]));
        }

        WebClient webClient = new WebClient();

        webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);

        webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(webClient_DownloadFileCompleted);

        stopWatch.Start();

        webClient.DownloadFileAsync(new Uri(Globals.ServerURL + Globals.OldFiles[curFile]), Globals.OldFiles[curFile]);
    }

    private static void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        currentBytes = lastBytes + e.BytesReceived;

        Common.ChangeStatus("DOWNLOADFILE", Globals.OldFiles[curFile], curFile.ToString(), Globals.OldFiles.Count.ToString());

        Common.UpdateCompleteProgress(Computer.Compute(Globals.completeSize + currentBytes));

        Common.UpdateCurrentProgress(e.ProgressPercentage, Computer.ComputeDownloadSpeed(e.BytesReceived, stopWatch));

    }

    private static void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
        lastBytes = currentBytes;

        Common.UpdateCurrentProgress(100, 0);

        curFile++;

        stopWatch.Reset();

        DownloadFile();
    }
}
类文件下载程序
{
私有静态文件;
私有静态长字节;
私有静态长字节;
专用静态秒表秒表=新秒表();
公共静态void下载文件()
{
if(Globals.OldFiles.Count=Globals.OldFiles.Count)
{
通用。更改状态(“下载完成”);
Common.StartGame();
返回;
}
if(Globals.OldFiles[curFile].Contains(“/”)
{
CreateDirectory(Path.GetDirectoryName(Globals.OldFiles[curFile]);
}
WebClient WebClient=新的WebClient();
webClient.DownloadProgressChanged+=新的DownloadProgressChangedEventHandler(webClient\u DownloadProgressChanged);
webClient.DownloadFileCompleted+=新的AsyncCompletedEventHandler(webClient\u DownloadFileCompleted);
秒表。开始();
webClient.DownloadFileAsync(新Uri(Globals.ServerURL+Globals.OldFiles[curFile]),Globals.OldFiles[curFile]);
}
私有静态void webClient_DownloadProgressChanged(对象发送方,DownloadProgressChangedEventArgs e)
{
currentBytes=lastBytes+e.BytesReceived;
Common.ChangeStatus(“DOWNLOADFILE”、Globals.OldFiles[curFile]、curFile.ToString()、Globals.OldFiles.Count.ToString());
Common.UpdateCompleteProgress(Computer.Compute(Globals.completeSize+currentBytes));
Common.UpdateCurrentProgress(例如ProgressPercentage,Computer.ComputedDownloadSpeed(例如BytesReceived,stopWatch));
}
私有静态void webClient_下载文件已完成(对象发送方,AsyncCompletedEventArgs e)
{
lastBytes=currentBytes;
Common.UpdateCurrentProgress(100,0);
curFile++;
秒表复位();
下载文件();
}
}

我不知道如何添加DownloadFileTaskAsync。任何帮助都将不胜感激。Thx

您应该知道限制在哪里。如果您使用所有可用的bandwith,并行下载实际上没有任何好处。但是如果你从许多不同的来源下载,你的bandwith比他们提供的要高,你可以并行下载。嗨@MrZach,答案是thx。bandwitch不是问题,它们大多是500kb以下的文件。问题是当前代码一次只下载一个文件。如果您能告诉我如何将异步下载实现到当前代码中,我将不胜感激。