Wait for download to complete

2019-08-02 19:29发布

问题:

I'm trying to create a simple program to download few files. I've tried some ready-made solutions I found on the web but I can't manage to make it work the way I want it to. I'm using this:

    private void startDownload(string toDownload, string saveLocation)
    {
        Thread thread = new Thread(() =>
        {
            WebClient client = new WebClient();
            client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
            client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
            client.DownloadFileAsync(new Uri(toDownload), saveLocation);
        });
        thread.Start();
    }

    void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        this.BeginInvoke((MethodInvoker)delegate
        {
            double bytesIn = double.Parse(e.BytesReceived.ToString());
            double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
            double percentage = bytesIn / totalBytes * 100;
            labelPercentage.Text = "Downloading " + Convert.ToInt32(percentage) + "%  -  " + Convert.ToInt32(bytesIn / 1024) + " / " + Convert.ToInt32(totalBytes / 1024) + " kB";
            progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());
        });
    }

    void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
        this.BeginInvoke((MethodInvoker)delegate
        {
            textBoxLog.AppendText("OK");
        });
    }

I'd like to make the program continue (download next file / show the "OK" message / do whatever is in the next line of code) AFTER the download has finished. In the current form, if I'd put eg.

    private void button1_Click(object sender, EventArgs e)
    {
        startDownload(url, localpath + @"\file.zip");
        textBoxLog.AppendText("the cake is a lie");
    }

it's showing me this text first and "OK" later.

I'm beginning with c#/.net and I've never learned object-oriented programming before so it's kind of double challenge for me and I can't figure it out by myself. I would be really grateful for relatively easy explanation.

回答1:

You can have startDownload wait for the asynchronous file download through Application.DoEvents() like this:

private bool downloadComplete = false;

private void startDownload(Uri toDownload, string saveLocation)
{
    string outputFile = Path.Combine(saveLocation, Path.GetFileName(toDownload.AbsolutePath));

    WebClient client = new WebClient();
    client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
    client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
    client.DownloadFileAsync(toDownload, outputFile);

    while (!downloadComplete)
    {
        Application.DoEvents();
    }

    downloadComplete = false;
}

void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
// No changes in this method...
}

void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
    this.BeginInvoke((MethodInvoker)delegate
    {
        textBoxLog.AppendText("OK");
        downloadComplete = true;
    });
}

And the download queue...

private void button1_Click(object sender, EventArgs e)
{
    FireDownloadQueue(urls, localpath);
    textBoxLog.AppendText("the cake is a lie");
}

private async void FireDownloadQueue(Uri[] toDownload, string saveLocation)
{
    foreach (var url in urls)
    {
        await Task.Run(() => startDownload(url, localpath));
    }
}

However, I think you're better off reading about HttpWebRequest and writing your own downloader class with proper checks and events... Here's a pretty good example by Hemanshu Bhojak (Source) that you can expand upon:

public class Downloader
{
    public async Task Download(string url, string saveAs)
    {
        var httpClient = new HttpClient();
        var response = await httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, url));
        var parallelDownloadSuported = response.Headers.AcceptRanges.Contains("bytes");
        var contentLength = response.Content.Headers.ContentLength ?? 0;

        if (parallelDownloadSuported)
        {
            const double numberOfParts = 5.0;
            var tasks = new List<Task>();
            var partSize = (long)Math.Ceiling(contentLength / numberOfParts);

            File.Create(saveAs).Dispose();

            for (var i = 0; i < numberOfParts; i++)
            {
                var start = i*partSize + Math.Min(1, i);
                var end = Math.Min((i + 1)*partSize, contentLength);

                tasks.Add(
                    Task.Run(() => DownloadPart(url, saveAs, start, end))
                    );
            }

            await Task.WhenAll(tasks);
        }
    }

    private async void DownloadPart(string url, string saveAs, long start, long end)
    {
        using (var httpClient = new HttpClient())
        using (var fileStream = new FileStream(saveAs, FileMode.Open, FileAccess.Write, FileShare.Write))
        {
            var message = new HttpRequestMessage(HttpMethod.Get, url);
            message.Headers.Add("Range", string.Format("bytes={0}-{1}", start, end));

            fileStream.Position = start;
            await httpClient.SendAsync(message).Result.Content.CopyToAsync(fileStream);
        }
    }
}

Example usage:

Task.Run(() => new Downloader().Download(downloadString, saveToString)).Wait();

With something along the lines of:

public class Downloader
{
    public event EventHandler DownloadProgress;
    DownloaderEventArgs downloaderEventArgs;

    public void DownloadStarted(DownloaderEventArgs e)
    {
        EventHandler downloadProgress = DownloadProgress;
        if (downloadProgress != null)
            downloadProgress(this, e);
    }

    // ...
}

class DownloaderEventArgs : EventArgs
{
    public string Filename { get; private set; }
    public int Progress { get; private set; }

    public DownloaderEventArgs(int progress, string filename)
    {
        Progress = progress;
        Filename = filename;
    }
    public DownloaderEventArgs(int progress) : this(progress, String.Empty)
    {
        Progress = progress;
    }
}


回答2:

startDownload initiates the download on a new thread, so when you invoke startDownload it starts the thread and the rest of the code after that continues immediately because it is on a separate thread. That is why you are seeing "the cake is a lie" before the "OK".