为Windows 8.1商店应用程序创建后台下载任务队列 [英] Creating background download task queue for windows 8.1 store app

查看:117
本文介绍了为Windows 8.1商店应用程序创建后台下载任务队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows 8.1商店应用。



我有一个下载任务,可以从Google云端硬盘下载后台文件。



有人可以帮我指点如何创建下载任务队列,也许可以获得下载任务的进度,并在任务在后台执行时显示在某个页面上。



感谢任何帮助。



谢谢,

迪帕克


我尝试过:



 < span class =code-keyword> public   static   void  StartDownload(DriveService service,File文件)
{
Task.Run( async ()= > < span class =code-keyword> await DownloadFile(service,file));
}


public static async 任务< IDownloadProgress> DownloadFile(DriveService服务,文件文件)
{
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);
}
返回进度;
}

解决方案

我确实看到了你对我之前问题的答案的所有评论,不幸的是,系统已经归结为支持评论等



一个最简单的方法,如果你只需要控制所有后台任务完成的时间,就可以使用任务。 WhenAll() Task.WaitAll()你不能等待无效),你可以等待这些异步调用做法。

  //  用于通用方法;  
var downloads = new List< Task>();

// 在集合中添加新的,
下载。添加(Task.Run(()= > {
// code。
}));

// 您可以将它们添加到循环或私有函数,处理程序等中。



然后等待他们所有人就像是,

 Task.WhenAll(downloads.ToArray( ));  //  等待 



检查进度,您可能希望运行逻辑以查看已下载的文件数量,速度等等。这有点算法 IProgress [ ^ ]界面可用于配置进度,但仅限于您需要。



如需深入了解,请阅读任务类(System.Threading.Tasks) [ ^ ]。

基于任务的异步模式(TAP) [ ^ ]

Task.WhenAll Method(Task [])(System.Threading.Tasks) [ ^ ]



这里只有1000英尺的概述, c# - 检查在后台任务中启动的下载操作的进度 - Stack Overflow [ ^ ]



为了更好地控制这个,哟你需要使用 Thread (它不像任务那么方便),但可以提供更多控制。


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:

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;
}

解决方案

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.

// 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,

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.


这篇关于为Windows 8.1商店应用程序创建后台下载任务队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆