locked
Progress report on BackgroundTransfer

    Question

  • Hi All,

    I create an unviersal app to download a file, about 10MB, using background transfer. The code I used is similer to the sdk sample here https://code.msdn.microsoft.com/windowsapps/Background-Transfer-Sample-d7833f61/sourcecode?fileId=52027&pathId=738703904, the code works OK to download the file.

                    Progress<DownloadOperation> progressCallback = new Progress<DownloadOperation>(DownloadProgress); 
                                                          await download.StartAsync().AsTask(cts.Token, progressCallback); 
               

    The problem is the progress reported bt the Progress object is not quite often. It seems to report every 1 mb data downloaded. It will take too long time to report progress every mb (10% of the total file) if my app is working on 3G or poor wifi network. Is there any settings / configs I can use to control how often to report?

    Thanks in advance.


    MSC on Computer Science (Parallel algorithm)

    Thursday, December 18, 2014 1:18 PM

Answers

  • Hi Yifeng,

    I cannot find how to setting reporting frequency when using background downloader, I think that class is used in background and the class provides properties to control the completion of work.

    I test to use the following code snippet, please try it, it will output when receiving data, you can modify the size of buff array to change the receiving buff size. You can also hold a variable to count the download progress.

    await  Task.Run(async () =>
                {
                    var target = https://code.msdn.microsoft.com/windowsapps/Windows-8-Modern-Style-App-Samples/file/99864/23/Windows%208.1%20Store%20app%20samples.zip;
                    var request = WebRequest.CreateHttp(target);
                    var t = Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);
                    var result = (HttpWebResponse)(await t);
                    var data = result.GetResponseStream();
                    var buff = new byte[5012];
                    int numRead = data.Read(buff, 0, buff.Length);
                    while (numRead > 0)
                    {
                        numRead = data.Read(buff, 0, buff.Length);
                        System.Diagnostics.Debug.WriteLine("{0} - Read {1} bytes", DateTime.Now.Ticks, numRead);                   
                    }
                });
    

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Tuesday, December 23, 2014 9:58 AM
    Moderator

All replies

  • Hi Li,

    Per my understanding, the BackgroundDownloader class is design for downloading the large file. It seems reporting progress when receiving about 1MB size. I think it is the BackgrondDownloader working style. So if you want to monitor the file download progress, I would recommend you implement your own way using HttpClient class.

    Please feel free to let me know if you have any concerns.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Friday, December 19, 2014 6:10 AM
    Moderator
  • Thanks Herro.

    The problem is if I use HttpClient or HttpWebRequest, it works OK on Win8.1 App, but have the problem on WP8.1. It seems all bytes are read into stream than reports the progress. This issue is also reported in thread https://social.msdn.microsoft.com/Forums/windowsapps/en-US/174e3de3-22aa-4dd1-bf74-2a6a66542dee/trying-to-stream-large-files-with-httpwebrequest-or-httpclient-in-wp81-results-in-unwanted-caching?forum=wpdevelop#174e3de3-22aa-4dd1-bf74-2a6a66542dee.

    Here is some code snip you can try:

     StorageFile file = null;
                DownloadFileResult result = null;
                Uri targetUri = null;
                WebResponse resp = null;
                HttpWebRequest request = null;
    
                try
                {
                    targetUri = new Uri(downloadUri);
    
                    request = HttpWebRequest.CreateHttp(targetUri);
    
                    resp = await request.GetResponseAsync();
    
                    long totalLength = resp.ContentLength;
    
    
                        file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
    
                        using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                        {
                            using (Stream responseStream = resp.GetResponseStream())
                            {
                                var buffer = new byte[4096];
                                int bytesRead;
                                long totalBytesRead = 0;
    
                                CancellationTokenSource cts = new CancellationTokenSource();
                                bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length, cts);
                                while (bytesRead > 0)
                                {
                                    totalBytesRead += bytesRead;
                                    stream.AsStream().Write(buffer, 0, bytesRead);
                                    if (progressNotify != null)
                                        progressNotify(totalBytesRead, totalLength);
    
                                       bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length, DownloadTaskQueue[fileName].Token);
                                }
                            }
                            stream.AsStream().Flush();
                        }
    
                        result = new DownloadFileResult() { TargetUri = targetUri, File = file, Size = totalLength, Result = DownloadResult.Success };
                    }
                
                catch (Exception e)

    For the reason, I shift to backgroundtransfer, it does report the progress on winphone, but per 1 MB.

    Any other suggestion or workaround?

    more reference links:

    https://social.msdn.microsoft.com/Forums/office/en-US/cc0b552a-9819-43df-8698-50ae8153a6bd/wp81-rt-problem-with-http-file-downloading?forum=wpdevelop

    http://stackoverflow.com/questions/23134363/asynchronous-download-not-reporting-via-iprogress-report

    Regards,

    Yifeng


    MSC on Computer Science (Parallel algorithm)


    • Edited by Yi Feng, Li Monday, December 22, 2014 6:02 AM
    Monday, December 22, 2014 5:07 AM
  • Hi Yifeng,

    I cannot find how to setting reporting frequency when using background downloader, I think that class is used in background and the class provides properties to control the completion of work.

    I test to use the following code snippet, please try it, it will output when receiving data, you can modify the size of buff array to change the receiving buff size. You can also hold a variable to count the download progress.

    await  Task.Run(async () =>
                {
                    var target = https://code.msdn.microsoft.com/windowsapps/Windows-8-Modern-Style-App-Samples/file/99864/23/Windows%208.1%20Store%20app%20samples.zip;
                    var request = WebRequest.CreateHttp(target);
                    var t = Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);
                    var result = (HttpWebResponse)(await t);
                    var data = result.GetResponseStream();
                    var buff = new byte[5012];
                    int numRead = data.Read(buff, 0, buff.Length);
                    while (numRead > 0)
                    {
                        numRead = data.Read(buff, 0, buff.Length);
                        System.Diagnostics.Debug.WriteLine("{0} - Read {1} bytes", DateTime.Now.Ticks, numRead);                   
                    }
                });
    

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Tuesday, December 23, 2014 9:58 AM
    Moderator