8

I'm creating a windows 10 UWP app, which involves BackgroundDownloader this works only in the desktop and not on the phone.

Code:

    var dl = new BackgroundDownloader();
    dl.CostPolicy = BackgroundTransferCostPolicy.Always;
    file = await localSoundsFolder.CreateFileAsync(name, CreationCollisionOption.ReplaceExisting);
    if (file != null)
    {
        var d = dl.CreateDownload(new Uri(uriToDownloadFrom,UriKind.RelativeOrAbsolute), file);
        d.Priority = BackgroundTransferPriority.High;

        var progressCallback = new Progress<DownloadOperation>(x => DownloadProgress(x, sc)); 
        try
        {
            await d.StartAsync().AsTask(cancellationToken.Token,progressCallback);
            //After this line it doesn't progress!
            CancellationTokenSource token = Utility.cancellationList[sc];
            if (token != null)
            {
                token.Cancel();
                Utility.cancellationList.Remove(sc);
                Debug.WriteLine("The sc has been removed from the download list");
            }
        }
        catch
        {
            return;
        }
    }


private static void DownloadProgress(DownloadOperation download,SoundClass sc)
{
    Debug.WriteLine("Callback");
    var value = download.Progress.BytesReceived * 100 / download.Progress.TotalBytesToReceive;
    Debug.WriteLine("The bytesReceived is {0} and total bytes is {1}", download.Progress.BytesReceived.ToString(), download.Progress.TotalBytesToReceive.ToString());
    new System.Threading.ManualResetEvent(false).WaitOne(10);
    sc.downloadProgress = value;
    if (download.Progress.Status == BackgroundTransferStatus.Completed || value >= 100)
    {
        Debug.WriteLine("DONE donwloading the file {0}", download.ResultFile.Name);
        Debug.WriteLine("The file name happened to be to be added was " + download.ResultFile.Name);
        string fileName = download.ResultFile.Name;
    }
}

After the line await d.StartAsync().AsTask(cancellationToken.Token,progressCallback); the program doesn't proceed. And there are no errors too. This is not working only on the phone works perfectly on the desktop! What am I missing?

AbsoluteSith
  • 1,917
  • 25
  • 60
  • it's just a tip but at application startup after previous termination you should enumerate all existing DownloadOperations and reattach them to current session. BackgroundDownloader does not support concurent downloads of the same URI, so if there is DownloadOperation hanging somewhere it may cause the problem. See https://msdn.microsoft.com/library/windows/apps/br207126 – Liero Nov 18 '15 at 15:09
  • Thanks for the tip Liero, tried this but its still not working. – AbsoluteSith Nov 18 '15 at 15:43
  • I tested your code in Mobile Emulator 10.0.10240 without `SoundClass` as I don't know what it is and your code works well. Are you testing in Emulator or in a real device? Here is a [Background transfer sample](https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BackgroundTransfer) from Microsoft, you can test it with the URI you used in your code to see if it works. – Jay Zuo Nov 19 '15 at 05:47
  • I tried with a different Uri which was a different source and it worked. But I was wondering what could be wrong on the server side as the Uri I generate is perfect and is working on browser and also the desktop mode of the app? Edit: Tried it on a different phone and it works there too. So I guess the issue is with the phone. – AbsoluteSith Nov 19 '15 at 15:16
  • @AbsoluteSith Did you solve it? I have the same problem on my Lumia 920. – Jakub Krampl Jan 22 '16 at 16:32
  • Well I tried it on a different phone and it worked. I'm not sure how I solved it on the phone maybe the updates fixed it. – AbsoluteSith Jan 23 '16 at 09:14
  • I know this is a slightly older post, but I was having the same issue and I think there may be a download file size limit that varies by device. I have a 32GB SD card and it won't pull down a 6GB file, but I was able to do multiple 4GB files from the same directory. – Jon Helms May 22 '16 at 14:26

3 Answers3

0

BackgroundDownloader and I think all BackgroundTasks in windows UWP hard to work with them . You have to first Create a new solution in your current solution as a Windows Runtime Component . After that you have to link it via Package.AppxManifest . Uh , Do not forget to make the runtime component as a reference of your main project . if you do these things hopefully it must be work . but be sure you have an RuntimeComponent and you linked it in to your project

Ali NGame
  • 449
  • 3
  • 14
  • 2
    I highly doubt we need a to create a BackgroundTask in order to have Background downloading. So this is not really a solution. – AbsoluteSith Jun 09 '16 at 12:21
0

We were struggling with exactly the same issue, but on a Universal Windows 10 app - not Phone. The culprit in our case was Windows 10's battery saver mode. On a Windows 10 device, tap the battery icon. This should bring up the battery and screen brightness flyout. Disable battery mode.

The effect was the same for any app using the Background downloader, including the MSDN download sample app.

Again, this was not on mobile but it did consistently happen on our Windows 10 app. Hope it's similar to phone.

nico van vuuren
  • 105
  • 1
  • 8
0

In my case the same code doesn't work in Debug mode by connecting the phone to the laptop with USB cable, but it works by downloading and installing the app in Release mode.

user2297037
  • 1,167
  • 2
  • 14
  • 34