0

これは私のコードです:

    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 つの更新プログラムのみをダウンロードし、解凍するアプリが必要です。

4

1 に答える 1

0

ブール値を使用すると、不正なソリューションを使用できます。

トップレベルのラムダを新しいasyncメソッドに移動する必要があります。通常の状態で呼び出すことができます。その後downloadCompleted、トップレベルのラムダの先頭でブール値を宣言し、falseに設定します。次に、に移動しAsyncCompletedEventHandler、最後にの値downloadCompletedをtrueに設定します。これにより、終了した最後のwhileループが通知されます。トップレベルのランバの終わりは、ダウンロードと圧縮が完全に完了するまで待つ必要があります。これにより、アプリケーションの状態が「応答なし」に変更されることはありません。

電話:

    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ループの呼び出しは、そのままにしておくことができます。

于 2012-12-15T18:35:17.227 に答える