xamarin.ios – 下载Xamarin Forms中的取消延迟

xamarin.ios – 下载Xamarin Forms中的取消延迟,第1张

概述我需要下载pdf文件并保存在设备中.我已经使用WebClient进程下载文件并在下载时显示进度. CancellationTokenSource Token= new CancellationTokenSource(); //Initialize a token while start downloadwebClient.DownloadFileTaskAsync(new Uri(downloa 我需要下载pdf文件并保存在设备中.我已经使用WebClIEnt进程下载文件并在下载时显示进度.

CancellationTokenSource Token= new CancellationTokenSource(); //Initialize a token while start downloadwebClIEnt.DownloadfileTaskAsync(new Uri(downloadurl),saveLocation); // Download file

下载工作正常.要取消正在进行的下载,我使用了如下所述的cancellationtokensource链接.

https://docs.microsoft.com/en-us/dotnet/standard/threading/cancellation-in-managed-threads

Token.Cancel(); //Cancellation downloadtry{// check whether download cancelled or notToken.ThrowIfCancellationRequested();if(Token.IsCancellationRequested){  //Changed button visibility}}catch (OperationCanceledException ex){}

取消下载需要几秒钟.你能建议我减少取消下载的延迟吗?

解决方法 我们必须在downloadasync进程之前将令牌注册到webclIEnt cancel async进程.我们必须保持如下的顺序,

//Initialize for download processWebClIEnt webClIEnt = new WebClIEnt();CancellationTokenSource token = new CancellationTokenSource();//register token into webclIEnttoken.Register(webClIEnt.CancelAsync);try{  webClIEnt.DownloadfileTaskAsync(new Uri(downloadurl),saveLocation); // Download a file}catch(Exception ex){  //Change button visibility}Token.Cancel(); //Cancellation download put in cancel click button event

它不需要甚至毫秒,并且取消在Xamarin.AndroID和Xamarin.iOS设备中都能正常工作.

总结

以上是内存溢出为你收集整理的xamarin.ios – 下载Xamarin Forms中的取消延迟全部内容,希望文章能够帮你解决xamarin.ios – 下载Xamarin Forms中的取消延迟所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1001382.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存