Domanda

We are currently downloading files using HttpClient because our backend requires certificate.

I have a control - FileRowwhich is an UI element with some code-behind methods for file downloading, like this one:

    if (FileIsDownloaded == false)
    {
        await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () =>
        {
            DataManager.Instance.DownloadFile(this);
        });
    }
    if (ThumbnailIsDownloaded == false)
    {
        await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () =>
        {
            DataManager.Instance.DownloadThumbnail(this);
        });
    }

Downloading single item is fine but when i click download all ( about 50 items ) the whole UI starts to freeze.

As you can see, i have tried to give requests low priority - but still same result.

Answers to common questions:

1) Yes the files should be downloadable all at one time, not one after another.
2) DataManager.Instance.DownloadThumbnail(this) - i do this to give refference to current control so that i could report a progress in a progress bar.

Any suggestions?

EDIT:

Downloading looks like this:

public async void DownloadFile(FileRow fileRow)
{
    //Lot of checking for if file exist, if version is the same
    string LocalFilename = await DownloadManager.DownloadFile(fileRow.MyFile.file.id, fileRow.MyFile.file.version, fileRow.MyFile.file.filename,fileRow);
    // next is just using the filename string
}

And finally my download:

public static async Task<string> DownloadFileOfCustomerAssetRow(int? id, int? version, string filename, FileRow fileRow)
    {
        try
        {
            HttpClientHandler aHandler = new HttpClientHandler();
            aHandler.ClientCertificateOptions = ClientCertificateOption.Automatic;
            HttpClient aClient = new HttpClient(aHandler);
            customerAssetRow.CurrentFileDownload = aClient;
            aClient.DefaultRequestHeaders.ExpectContinue = false;
            HttpResponseMessage response = await aClient.GetAsync(WebServices.BackendStartUrl + "getFileData?id=" + id + "&version=" + version, HttpCompletionOption.ResponseHeadersRead);
            var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, Windows.Storage.CreationCollisionOption.GenerateUniqueName);
            fileRow.FileName = file.Name;
            using (var fs = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
            {
                Stream stream = await response.Content.ReadAsStreamAsync();
                IInputStream inputStream = stream.AsInputStream();
                ulong totalBytesRead = 0;
                while (true)
                {
                    IBuffer buffer = new Windows.Storage.Streams.Buffer(1024);
                    buffer = await inputStream.ReadAsync(
                        buffer,
                        buffer.Capacity,
                        InputStreamOptions.None);
                    if (buffer.Length == 0)
                    {
                        break;
                    }
                    totalBytesRead += buffer.Length;
                    fileRow.Progress.Value = fileRow.Progress.Value + 1024;
                    await fs.WriteAsync(buffer);
                }
                inputStream.Dispose();
            }
            fileRow.Progress.Visibility = Visibility.Collapsed;
            return file.Name;
        }
        catch (Exception e)
        {
            ErrorReporter.ReportError("Error in DownloadManager.cs in function DownloadFile.", e);
            return "";
        }
    }
È stato utile?

Soluzione

It's possible that your async method continuations are interrupting the UI thread too much. Try creating a stronger separation between your background logic (DownloadFileOfCustomerAssetRow) and your UI (FileRow) by introducing an IProgress<T> reporter. Then ensure that every await in your background logic has a ConfigureAwait(false) on it.

public static async Task<string> DownloadFileOfCustomerAssetRow(int? id, int? version, string filename, IProgress<int> progress)
{
    HttpClientHandler aHandler = new HttpClientHandler();
    aHandler.ClientCertificateOptions = ClientCertificateOption.Automatic;
    HttpClient aClient = new HttpClient(aHandler);
    customerAssetRow.CurrentFileDownload = aClient;
    aClient.DefaultRequestHeaders.ExpectContinue = false;
    HttpResponseMessage response = await aClient.GetAsync(WebServices.BackendStartUrl + "getFileData?id=" + id + "&version=" + version, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
    var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, Windows.Storage.CreationCollisionOption.GenerateUniqueName).ConfigureAwait(false);
    fileRow.FileName = file.Name;
    using (var fs = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite).ConfigureAwait(false))
    {
        Stream stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
        IInputStream inputStream = stream.AsInputStream();
        ulong totalBytesRead = 0;
        while (true)
        {
            IBuffer buffer = new Windows.Storage.Streams.Buffer(1024);
            buffer = await inputStream.ReadAsync(
                buffer,
                buffer.Capacity,
                InputStreamOptions.None).ConfigureAwait(false);
            if (buffer.Length == 0)
            {
                break;
            }
            totalBytesRead += buffer.Length;
            if (progress != null)
                progress.Report(totalBytesRead);
            await fs.WriteAsync(buffer).ConfigureAwait(false);
        }
        inputStream.Dispose();
    }
    return file.Name;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top