Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am working on this Windows 8.1 store app.

I am trying to download a file from google drive using the google-drive-api v3 in background without it being interrupted if I navigate to some other page.

Below is what I am doing to download a file but I am not sure what I need to do to make the same work in background.

Can someone please help me how I can do this same in background task.

Thanks,

What I have tried:

C#
public  async Task DownloadFile(DriveService service, File file)
        {
            var request = service.Files.Get(file.Id);
            var stream = new MemoryStream();
            if (file.MimeType != "application/vnd.google-apps.folder")
            {
                request.MediaDownloader.ProgressChanged += (IDownloadProgress progress) =>
                {
                    switch (progress.Status)
                    {
                        case DownloadStatus.Downloading:
                            {
                                Debug.WriteLine(progress.BytesDownloaded);
                                break;
                            }
                        case DownloadStatus.Completed:
                            {
                                Debug.WriteLine("Download complete.");
                                break;
                            }
                        case DownloadStatus.Failed:
                            {
                                Debug.WriteLine("Download failed.");
                                break;
                            }
                    }
                };
                request.Download(stream);
                await SaveToLocalFolderAsync(stream, file.Name);
            }
        }
Posted
Updated 23-Dec-16 7:24am

1 solution

You already have an async function, just add a Task.Run[^] function in the body to run the task in the background, that will just require to wrap the function code in there, and execute it.
C#
public void func() {
   Task.Run(async () => {
      // awaitable code here.
   });
}

You might need to change the inner function to async too, just read the link provided above, it has examples for your need.

I recommend that you try to read this, Asynchronous Programming with async and await (C#)[^].
 
Share this answer
 
Comments
deepaksharma0390 25-Dec-16 9:32am    
This works.Thanks.
I have another question. Is there a way I can keep track of these tasks and display the statuses maybe on some page(like some queue). Any pointer on how I can implement that ?
Appreciate your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900