2

URLからファイルをダウンロードするためにBackgroundDownloaderを使用しています。各ダウンロードのパーセンテージ (1%、2%、3% など) を進行状況バーに表示し、テキストとして表示する必要があります。しかし、ダウンロードごとに(1つのファイルのみ)、大量のダウンロード率(40%、60%など)を取得しています。これが私のコードです:

private async void btnDownload_Click(object sender, RoutedEventArgs e)
    {
        Uri source;
        StorageFile destinationFile;
        StorageFolder folder;
        string destination = "SampleImage.png";
        if (!Uri.TryCreate(txtUrl.Text.Trim(), UriKind.Absolute, out source))
        {
            txtUrl.Text = "Pls provide correct URL...";
            return;
        }
        try
        {
            folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("SampleFolder", CreationCollisionOption.OpenIfExists);
            destinationFile = await folder.CreateFileAsync(destination, CreationCollisionOption.GenerateUniqueName);
        }
        catch
        {
            txtProgress.Text = "Opss something went wrong... try again....";
            return;
        }

        BackgroundDownloader downloader = new BackgroundDownloader();
        DownloadOperation download = downloader.CreateDownload(source, destinationFile);

        if (download != null)
        {
            try
            {
                var progress = new Progress<DownloadOperation>(ProgressCallback); // for showing progress
                await download.StartAsync().AsTask(cancelProcess.Token, progress);
            }
            catch (TaskCanceledException)
            {
                txtProgress.Text = "Canceled";
            }
            catch(Exception)
            {
                txtProgress.Text = "Something went wrong pls try again....";
            }
        }
    }
//for showing progress
private void ProgressCallback(DownloadOperation obj)
    {
        double progress = 0;
        if (obj.Progress.BytesReceived > 0)
        {
            progress = obj.Progress.BytesReceived * 100 / obj.Progress.TotalBytesToReceive;
            if (progress > 0)
            {
                txtProgress.Text = string.Format("Downloading your file.... {0}%", progress);
                pbDownloading.Value = progress; // passing progress bar value
            }
        }
        else
        {
            txtProgress.Text = "Check your internet connection...";
        }
    }

これを使用して、ダウンロードのすべての進捗率を取得するにはどうすればよいですか...? またはこれを行うための他の最良の方法...?

4

1 に答える 1

2

そのため、ダウンロードの進行状況が急激に変化するのではなく、スムーズに変化する必要があります (整数パーセントで測定)。次に、生のダウンロード進行状況をそのまま表示するのではなく、表示される進行状況を 1% ずつインクリメントするメソッド ( nextPercent) を作成し、ダウンロード速度に比例する頻度で呼び出す必要があります。

まず、ダウンロード状態を確認するタイマーを設定する必要があります。タイマーの頻度は 1 秒あたり約 10 ティックで、これがダウンロードの進行状況の更新速度です。ダウンロード ハンドラーは、内部変数int DownloadPercentを更新し、ミリ秒あたりのパーセントでダウンロード速度を測定する必要があります。double DownloadSpeed = DownloadPercent/(DateTime.Now - DownloadStartTime).TotalMilliseconds;
次に、DispatcherTimer コールバックはダウンロードの進行状況を 1 秒あたり 10 回チェックし、表示された進行状況が実際よりも小さく、最後の UI 更新から十分な時間が経過した場合は nextPercent を呼び出します。では、十分な時間をどのように判断しますか。

DateTime lastUIUpdate; //class variable, initialized when download starts and UI is set to 0%
int DisplayedPercent;

void nextPercent(object sender, object args) {
    if (DisplayedPercent == DownloadPercent) return;

    double uiUpdateSpeed = (DateTime.Now - lastUIUpdate).TotalMilliseconds / (DisplayedPercent + 1);
    if (uiUpdateSpeed < DownloadSpeed) {
         nextPercent();
    }
}

これには多少の調整が必要になると確信していますが、アイデアを得る必要があります。幸運を!

于 2012-11-01T12:39:48.307 に答える