如何在后台任务中从Google云端硬盘下载文件 [英] How to download files from Google drive in background task

查看:141
本文介绍了如何在后台任务中从Google云端硬盘下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我试图在后台使用google-drive-api v3从谷歌驱动器下载文件如果我导航到其他页面,它会被打断。



以下是我正在做的下载文件,但我不知道我需要做什么来制作同样的工作在后台。



有人可以帮助我如何在后台任务中做同样的事情。



谢谢,



我尝试过:



  public   async 任务DownloadFile(DriveService服务,文件文件)
{
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( 下载完成。);
break ;
}
case DownloadStatus.Failed:
{
Debug.WriteLine( 下载失败。);
break ;
}
}
};
request.Download(stream);
await SaveToLocalFolderAsync(stream,file.Name);
}
}

解决方案

您已经有了异步功能,只需添加一个 Task.Run [ ^ ]函数在正文中运行后台任务,只需要将函数代码包装在那里,然后执行它。

  public   void  func(){ 
Task.Run( async ()= > {
// 此处有等待代码。
});
}



您可能还需要将内部函数更改为异步,只需阅读上面提供的链接,它就有您需要的示例。



我建议您尝试阅读本文,使用异步和等待(C#)进行异步编程 [ ^ ]。


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:

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

解决方案

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.

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#)[^].


这篇关于如何在后台任务中从Google云端硬盘下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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