Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a Windows 8.1 store app.

I have a Download task which downloads a file in background from Google Drive.

Can someone please help me with any pointers how I can create download task queues and maybe get the progress of the download tasks and display it on some page while the tasks are getting executed in the background.

Appreciate any help.

Thanks,
Deepak

What I have tried:

C#
public static void StartDownload(DriveService service, File file)
{
	Task.Run(async () => await DownloadFile(service, file));
}


public static async Task<IDownloadProgress> DownloadFile(DriveService service, File file)
{
    var request = service.Files.Get(file.Id);
    var stream = new MemoryStream();
    IDownloadProgress progress = null;
    if (file.MimeType != "application/vnd.google-apps.folder")
    {
        progress = await request.DownloadAsync(stream, CancellationToken.None);
        SaveToLocalFolderAsync(stream, file.Name);
    }
    return progress;
}
Posted
Updated 25-Dec-16 10:31am

1 solution

I did see all of your comments on my answer to your previous question, unfortunately, system is down to support comments etc.

A most simple method, if you just need to control when all of the background tasks finish, you can use Task.WhenAll() or Task.WaitAll() (you cannot await void), you can await these calls for asynchronous approach.
C#
// for a generic approach;
var downloads = new List<Task>();

// Add new ones to collection, 
downloads.Add(Task.Run(() => {
   // code.
}));

// You can add them in a loop, or private function, handler etc.

Then awaiting them all will be like,
C#
Task.WhenAll(downloads.ToArray()); // waits

To check the progress, you might want to run the logic to see how much file has been downloaded, what the speed is etc. That is a bit algorithmic. IProgress[^] interface can be used to configure the progress, but only if you need.

For even in-depth overview, read Task Class (System.Threading.Tasks)[^].
Task-based Asynchronous Pattern (TAP)[^]
Task.WhenAll Method (Task[]) (System.Threading.Tasks)[^]

Just a 1000ft overview is here, c# - Check progress of download operation which started in background task - Stack Overflow[^]

For more control over this, you will need to use Thread (which are not as much handier as Task), but gives more control.
 
Share this answer
 
Comments
deepaksharma0390 30-Dec-16 9:50am    
Is there a way I get fetch the list of Tasks running ? I am trying to fetch the list of tasks on Page2 which I ran using
Task.Run
while I ran these tasks from Page1.
Is there any Context which I can use to get the same ?
Thanks
Afzaal Ahmad Zeeshan 30-Dec-16 14:01pm    
You can add them to a collection on a static shared object, that can be accessed on any page, then pass them there. What sort of application do you have? Try having a separate class in there.
deepaksharma0390 31-Dec-16 8:46am    
It is Windows 8.1 UWP app.
Afzaal Ahmad Zeeshan 31-Dec-16 10:16am    
Then store it under App object of your application. Create a static property to hold List<Task> objects.
deepaksharma0390 31-Dec-16 13:07pm    
Really thanks for the insight. Did not think that. Let me try that out and see.
Thanks

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