locked
Windows RT: process download when app is suspended?

    Question

  • Dear alls,

    I am beginner for Windows RT. I have problem with download process of Windows RT. I implemented download on my project, but when app is suspended then download progress stop. I thought about two approaches, but I find problem with each.

    1) I thought use BackgroundDownloader to run background for download, but BackgoundDownloader just download for direct link (ex: abc.com//abc/file.mp3). In my app, I process download by get binary stream after request to server, so I think I can not use BackgroundDownloader in that case.

    2) Use Window RT component, but with that way, I can not iterate between main app and Windows RT component for updating DownloadQueue in main App.

    var device = new DownloadDevice();
                device.DeviceId = AppConfig.Instance.GetValueString(ConfigKey.DeviceID);
                device.TokenId = tokenId;
    
                HttpRequestMessage reqmessage = null;
                using (var ms = new MemoryStream())
                {
                    var serial = new DataContractJsonSerializer(typeof(DownloadDevice));
                    serial.WriteObject(ms, device);
                    entityBody = Encoding.UTF8.GetString(ms.ToArray(), 0, ms.ToArray().Length);
                    reqmessage = new HttpRequestMessage(HttpMethod.Post, downloadUri);
                    reqmessage.Content = new StringContent(entityBody);
                    reqmessage.Version = new Version(1, 0); // HTTP 1.0
                    reqmessage.Headers.Add("Connection", "close"); // don't Keep-Alive              
                }
    
                var oAuth = new OAuth();
                var uri = new Uri(downloadUri);
                using (var req = new HttpClient())
                {
                    req.Timeout = new TimeSpan(48, 0, 0); // 48h = CloudFront Timeout
    
                    var vAuthHeader = oAuth.AuthorizationHeader(uri, "POST");
                    req.BaseAddress = uri;
                    req.DefaultRequestHeaders.Add("Authorization", vAuthHeader);
                    req.DefaultRequestHeaders.Add("user-agent", USER_AGENT);
                    var res = await req.SendAsync(reqmessage, HttpCompletionOption.ResponseHeadersRead, cts.Token);
    
                    if (res != null && res.StatusCode == HttpStatusCode.OK)
                    {
                        try
                        {
                            var contents = await res.ReadBinalyAsync(progress, cts);
                            return contents;
                        }
                        catch
                        {
                            throw;
                        }
                    }
                    else if (res != null && res.StatusCode != HttpStatusCode.OK)
                    {
                        throw new HttpRequestException(res.StatusCode.ToString());
                    }
                    else
                    {
                        throw new HttpRequestException();
                    }
                }

    Please help me. Thanks.

    • Edited by kanozuki Thursday, March 13, 2014 5:01 AM
    Thursday, March 13, 2014 5:00 AM

Answers

  • The BackgroundDownloader is the way to go. It's the only reasonable way to continue the download while the app is suspending (other background tasks are limited in how long and often they can run). You can download to a file and then open it into a stream when the app runs after the download has completed.

    --Rob

    • Marked as answer by kanozuki Monday, March 17, 2014 7:24 AM
    Thursday, March 13, 2014 5:08 AM
    Owner
  • Thanks for your reply, Rob.

    I have read many samples use BackgroundDownloader, I see uri is used in these cases is direct link to file (as I said), but my uri isn't same (just http://xxx/xxx/token). Moreover, I have to implement authentication step with server (https), whether BackgroundDownloader has this step?. Can I use BackgroundDownloader?


    If have, can you post sample code about using BackgroundDownloader which have authentication? Please.
    • Edited by kanozuki Thursday, March 13, 2014 6:50 AM
    • Marked as answer by kanozuki Monday, March 17, 2014 7:22 AM
    Thursday, March 13, 2014 6:36 AM

All replies

  • The BackgroundDownloader is the way to go. It's the only reasonable way to continue the download while the app is suspending (other background tasks are limited in how long and often they can run). You can download to a file and then open it into a stream when the app runs after the download has completed.

    --Rob

    • Marked as answer by kanozuki Monday, March 17, 2014 7:24 AM
    Thursday, March 13, 2014 5:08 AM
    Owner
  • Thanks for your reply, Rob.

    I have read many samples use BackgroundDownloader, I see uri is used in these cases is direct link to file (as I said), but my uri isn't same (just http://xxx/xxx/token). Moreover, I have to implement authentication step with server (https), whether BackgroundDownloader has this step?. Can I use BackgroundDownloader?


    If have, can you post sample code about using BackgroundDownloader which have authentication? Please.
    • Edited by kanozuki Thursday, March 13, 2014 6:50 AM
    • Marked as answer by kanozuki Monday, March 17, 2014 7:22 AM
    Thursday, March 13, 2014 6:36 AM
  • Hi Rob,

    Thanks for you answer. I could get file by BackgroundDowloader.

    Best Regards,

    Le Khanh.

    Monday, March 17, 2014 7:23 AM