等到下载完成

时间:2012-12-15 18:01:01

标签: c# .net

这是我的代码:

    Action<int, ProgressBar, Label, Label, int, Button> downloadFileAsync = (i, pb, label2, label1, ServID, button1) =>
    {
        var bd = AppDomain.CurrentDomain.BaseDirectory;
        var fn = bd + "/" + i + ".7z";
        var down = new WebClient();
        DownloadProgressChangedEventHandler dpc = (s, e) =>
        {
            label1.Text = "Download Update: " + i + " / " + ServID;
            int rec =Convert.ToInt16(e.BytesReceived / 1024);
            int total =Convert.ToInt16(e.TotalBytesToReceive / 1024)  ;
            label2.Text = "Downloaded: " + rec.ToString() + "KB / " + total.ToString() + " KB";
            pb.Value = e.ProgressPercentage;
        };
        AsyncCompletedEventHandler dfc = null;  dfc = (s, e) =>
        {
            label1.Text = "Extracting Files...";
            Rar Ra = new Rar();
            Ra.Open(i + ".7z");
            Ra.Unrar(AppDomain.CurrentDomain.BaseDirectory);
            File.Delete(fn);
            down.DownloadProgressChanged -= dpc;
            down.DownloadFileCompleted -= dfc;
            down.Dispose();
            if (ServID == i)
            {
                button1.Enabled = true;
                label1.Text = "Have Fun In-Game...";
                label2.Text = "Done...";
            }
        };
        down.DownloadProgressChanged += dpc;
        down.DownloadFileCompleted += dfc;
        down.DownloadFileAsync(new Uri("http://unknowndekaron.net/updatee/" + i + "/" + i + ".7z"), fn);
    };

这是我的电话:

                while (i <= ServID)
                {
                    downloadFileAsync(i, progressBar1, label2, label1, ServID, button1);
                    i++;
                }

解压缩:

    public void Decompress(int i)
    {

        Rar Ra = new Rar();
        Ra.Open(i + ".7z");
        Ra.Unrar(AppDomain.CurrentDomain.BaseDirectory);

    }

在此代码程序中一次下载所有更新... 它开始解密完成下载完成的第一个。 我需要应用程序一次只下载一个更新然后解压缩它。

1 个答案:

答案 0 :(得分:0)

使用布尔值可以使用不优雅的解决方案:

您应该将顶级lambda移动到新的async方法中。您可以在正常的情况下调用它。之后在顶级lambda的开头声明布尔downloadCompleted并将其设置为false。然后转到AsyncCompletedEventHandler,最后将downloadCompleted的值设置为true - 这将表示我们已完成的最后一个while循环。顶级lamba的结束应该等到下载和压缩完成。这不会将应用程序的状态更改为“无响应”。

示例

呼叫:

    downloadFileAsync = (i, pb, label2, label1, ServID, button1) =>
    {
        Download(i, pb, label2, label1, ServID, button1);
    };

新方法:

    public async Task<int> Download(...)
    {
        bool downloadCompleted = false;
        var bd = AppDomain.CurrentDomain.BaseDirectory;
        var fn = bd + "/" + i + ".7z";
        var down = new WebClient();
        DownloadProgressChangedEventHandler dpc = (s, e) =>
        {
            label1.Text = "Download Update: " + i + " / " + ServID;
            int rec = Convert.ToInt16(e.BytesReceived / 1024);
            int total = Convert.ToInt16(e.TotalBytesToReceive / 1024);
            label2.Text = "Downloaded: " + rec.ToString() + "KB / " + total.ToString() + " KB";
            pb.Value = e.ProgressPercentage;
        };
        AsyncCompletedEventHandler dfc = null; dfc = (s, e) =>
        {
            label1.Text = "Extracting Files...";
            Rar Ra = new Rar();
            Ra.Open(i + ".7z");
            Ra.Unrar(AppDomain.CurrentDomain.BaseDirectory);
            File.Delete(fn);
            down.DownloadProgressChanged -= dpc;
            down.DownloadFileCompleted -= dfc;
            down.Dispose();
            if (ServID == i)
            {
                button1.Enabled = true;
                label1.Text = "Have Fun In-Game...";
                label2.Text = "Done...";
            }
            downloadCompleted = true;
        };
        down.DownloadProgressChanged += dpc;
        down.DownloadFileCompleted += dfc;
        down.DownloadFileAsync(new Uri("http://unknowndekaron.net/updatee/" + i + "/" + i + ".7z"), fn);
        while (!downloadCompleted)
            ;
    }

while循环调用可以保持不变。