0

チェックボックスがチェックされているかどうかに応じて、Webサイトから複数のファイルをダウンロードする必要があるこのアプリケーションを作成しています。ファイルのダウンロードに DownloadFileAsync メソッドを使用しています。

私が抱えている問題は、最初のファイルのダウンロードが開始されることです。コードの残りの部分に続きます。例えば。ダウンロードが終了する前にリストボックスに「1」を追加し、次のifステートメントに移動してダウンロードを実行し、ダウンロードが開始されるとすぐにリストボックスに「2」を追加します。以下は私が使用しているコードです。

private void button1_Click(object sender, EventArgs e)
        {

            if (checkBox1.Checked)
            {
                WebClient client = new WebClient();
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
                client.DownloadFileAsync(new Uri("https://speed.hetzner.de/100MB.bin"), "100mb");
                listBox1.Items.Add("1");
            }
             if (checkBox2.Checked)
            {
                WebClient client = new WebClient();
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
                client.DownloadFileAsync(new Uri("https://speed.hetzner.de/100MB.bin"), "200mb");
                listBox1.Items.Add("2");
            }
        }


        void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            double bytesIn = double.Parse(e.BytesReceived.ToString());
            double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
            double percentage = bytesIn / totalBytes * 100;

            progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());
        }

        void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            progressBar1.Value = 0;
        }

async と wait を使用してみましたが、動作しませんでした。つまり、コードで最初のファイルを完全にダウンロードし、リストボックスに「1」を追加してから、2番目のifステートメントに移動して2番目のファイルをダウンロードするにはどうすればよいですか。

前もって感謝します。

4

0 に答える 0