0

@Enigmativityがここに書いたことをしました:

        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 + " From: " + ServID;
            int rec =Convert.ToInt16(e.BytesReceived / 1024);
            int total =Convert.ToInt16(e.TotalBytesToReceive / 1024)  ;
            label2.Text = "Downloaded: " + rec.ToString() + " / " + total.ToString() + " KB";
            pb.Value = e.ProgressPercentage;
        };
        AsyncCompletedEventHandler dfc = null;  dfc = (s, e) =>
        {
            down.DownloadProgressChanged -= dpc;
            down.DownloadFileCompleted -= dfc;
            CompressionEngine.Current.Decoder.DecodeIntoDirectory(AppDomain.CurrentDomain.BaseDirectory + "/" + i + ".7z", AppDomain.CurrentDomain.BaseDirectory);
            File.Delete(fn);
               if (i == ServID)
                {

                   button1.Enabled = true;
                   label1.Text = "Game Is Up-To-Date.Have Fun!!";
                  label2.Text = "Done..";
               }
         down.Dispose();
        };

私の唯一の問題は、プログラムがダウンロードされたファイルを拡張しているときです

CompressionEngine.Current.Decoder.DecodeIntoDirectory(AppDomain.CurrentDomain.BaseDirectory + "/" + i + ".7z", AppDomain.CurrentDomain.BaseDirectory);

一部のファイルでは、ダウンロードしたファイルを削除するのに時間がかかるため、解凍が完了するまでプログラムに待機するように指示するにはどうすればよいですか?

4

1 に答える 1

2

単一の非同期ダウンロードをカプセル化する単一のラムダを定義してから、それをループで呼び出してみてください。

これがラムダです:

Action<int> downloadFileAsync = i =>
{
    var bd = AppDomain.CurrentDomain.BaseDirectory;
    var fn = bd + "/" + i + ".7z";
    var wc = new WebClient();
    DownloadProgressChangedEventHandler dpc = (s, e) =>
    {
        progressBar1.Value = e.ProgressPercentage;
    };
    AsyncCompletedEventHandler dfc = null;
    dfc = (s, e) =>
    {
        wc.DownloadProgressChanged -= dpc;
        wc.DownloadFileCompleted -= dfc;
        CompressionEngine.Current.Decoder.DecodeIntoDirectory(fn, bd);
        File.Delete(fn);
        wc.Dispose();
    };
    wc.DownloadProgressChanged += dpc;
    wc.DownloadFileCompleted += dfc;
    wc.DownloadFileAsync(new Uri(Dlpath + i + "/" + i + ".7z"), fn);
};

すべてのイベントを適切に切り離し、WebClientインスタンスを正しく破棄することに注意してください。

今それをこのように呼んでください:

while (i <= ServID)
{
    downloadFileAsync(i);
    i++;
}

すべてのファイルのダウンロードの進行状況を適切に表示するには、進行状況バーの更新をいじる必要がありますが、原則として、これでうまくいくはずです。

于 2012-12-02T08:33:01.567 に答える