ファイルをダウンロードする C# Windows アプリケーションを作成しようとしています (そのダウンロードが完了するのを待ちます - 進行状況のインジケーターを提供します)。ダウンロードしたファイルを実行します。私はどちらかが必要だと思います:
- 進行状況インジケーターがあり、アプリケーションが応答しなくなることのない同期ダウンロード
- ダウンロードが完了するのを待ってから実行を試みる非同期ダウンロード。
前者 (同期ダウンロード) は私が望むことをしているように見えますが、進行状況を示す方法が見つからず、プログラムが応答しなくなっているようです (ハングしたように) - ユーザーが代わりにアプリケーションを閉じる可能性がありますダウンロードが完了するのを待ちます。
後で(非同期ダウンロード)進行状況インジケーターなどで達成できましたが、ダウンロードが開始され、ダウンロードが完了する前にアプリケーションがすぐにインストールを試みますが、もちろん失敗します。
それで、これを達成するための最良の方法は何ですか?以下は、進行状況バー付きの非同期ダウンロード用に現在持っているコードです。
2012 年 4 月 10 日編集
古いコードは回避するのが面倒になってきていたので、ここに私が現在持っているものがあります。プログレスバーを更新しないことを除いて動作します。
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
List<App> FilesToDownload = e.Argument as List<App>;
foreach (App Program in FilesToDownload) // Loop through List with foreach
{
//string Text = ;
UpdateRichTextBox("Downloading " + Program.Filename + "... Please wait");
string Source = Program.DownloadLocation + "/" + Program.Filename;
string Destination = System.AppDomain.CurrentDomain.BaseDirectory + Program.Filename;
WebClient webClient = new WebClient();
webClient.DownloadFile(Source, @Destination);
}
UpdateRichTextBox("File download complete");
foreach (App Program in FilesToDownload) // Loop through List with foreach
{
string Filename = Program.Filename;
string Arguments = Program.InstallParameters;
if (Filename != "advisorinstaller.exe")
{
Process p = new Process();
p.StartInfo.FileName = System.AppDomain.CurrentDomain.BaseDirectory + Filename;
p.StartInfo.Arguments = Arguments;
p.Start();
p.WaitForExit();
}
else
{
MessageBox.Show("About to install Belarc Advisor. This may take 10-15 minutes to run - do not shutdown this program. Click OK when ready to proceed.");
}
}
}
private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
UpdateRichTextBox("Install Complete");
}
2012 年 4 月 11 日を編集
したがって、バックグラウンドワーカーが次のように機能するように編集しました。
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
List<App> FilesToDownload = e.Argument as List<App>;
int counter = 0;
int percent = 0;
foreach (App Program in FilesToDownload) // Loop through List with foreach
{
percent = ((counter / FilesToDownload.Count) * 100);
backgroundWorker1.ReportProgress(percent, "");
UpdateRichTextBox("Downloading " + Program.Filename + "... Please wait");
string Source = Program.DownloadLocation + "/" + Program.Filename;
string Destination = System.AppDomain.CurrentDomain.BaseDirectory + Program.Filename;
WebClient webClient = new WebClient();
webClient.DownloadFile(Source, @Destination);
counter++;
}
//backgroundWorker1.ReportProgress(100, "Complete!");
}
最後の行のコメントを外すと、プログレス バーは 100% に進みます。しかし、それを使用して進行していません:
percent = ((counter / FilesToDownload.Count) * 100);
backgroundWorker1.ReportProgress(percent, "");
理由はありますか?