当下载期间设备丢失网络时,[uwp]流不会抛出错误。 [英] [uwp]stream not throw error when device losses network during download.

查看:70
本文介绍了当下载期间设备丢失网络时,[uwp]流不会抛出错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用代码从流中下载数据并将数据写入存储文件。它工作正常,但我希望每当设备在下载过程中丢失网络时循环证明错误,这样我就可以将下载状态从进度更改为失败

I am using code to download data from stream and write data to storage file. it is working fine but I want whenever device losses network during download while loop prove error so that I can change status of downloading from progress to failed

我的代码冻结在下面的行无网络

my code freeze at below line on no network

while((read = await inputStream.ReadAsync(buffer,0,buffer.Length))> 0)

while ((read = await inputStream.ReadAsync(buffer, 0, buffer.Length)) > 0)

使用(VAR的inputStream =(AWAIT client.GetInputStreamAsync(contentUri).AsTask(cancellationToken.Token,downloadFileItem.Progress))AsStreamForRead()。)

 &NBSP ;                  {

                    &NBSP ;  使用(VAR的OutputStream =(等待contentFile.OpenAsync(FileAccessMode.ReadWrite))AsStreamForWrite()。)

          &NBSP ;             {

                    &NBSP ;       VAR缓冲液=新的字节[1024 * 1024];

               &NBSP ;            INT读;

                   &NBSP ;        downloadFileItem.Status = PDSConstants.DOWNLOAD_STATUS_PROGRESS_TEXT;

                             while((read = await inputStream.ReadAsync(buffer,0,buffer.Length))> 0)///代码冻结在这里没有网络


                             {

                    &NBSP ;          如果

&NBSP(IsDownloadToastAllreadyDisplayed!);                 &NBSP ;             {

                    &NBSP ;               IsDownloadToastAllreadyDisplayed = TRUE;

                                     if(!isRetryingDownload)

                    ;                     PDSUtility.ShowToast(QUOT;松下
数字标牌与QUOT;,"内容下载开始");



     &NBSP ;                        &NBSP ; }

                    &NBSP ;           outputStream.Seek(0,SeekOrigin.End);

               &NBSP ;                await outputStream.WriteAsync(buffer,0,read);

                                 downloadFileItem.ProgressVisibility = Visibility.Visible;

                                 downloadFileItem.Progress.BytesReceived + =(ULONG)读取;

                                 downloadFileItem.TotalDownloadedSize + =读;

                 &NBSP ;             如果(downloadFileItem.Progress.TotalBytesToReceive / 1024> 1024)//
如果文件大小大于1 MB

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; downloadFileItem.Status = String.Format(< {0}
of {1} mb。已下载 - {2}%完成。",Math.Round(((size + downloadFileItem.Progress.BytesReceived)/ 1024F )/ 1024F,2),Math.Round(Convert.ToDecimal(((大小+ downloadFileItem.Progress.TotalBytesToReceive)/ 1024F)/ 1024F),2),downloadFileItem.ProgressPercentage);

&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;否则

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; downloadFileItem.Status =的String.Format(QUOT; {0} $ B $ {1} kb的下载b - {2}%完整" ;, Math.Round((大小+ downloadFileItem.Progress.BytesReceived)/ 1024F, 2),Math.Round(Convert.ToDecimal((size + downloadFileItem.Progress.TotalBytesToReceive)/ 1024F),2),downloadFileItem.ProgressPercentage);
$


  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP; downloadFileItem.ProgressPercentage =(int)的(100 *((双)downloadFileItem.TotalDownloadedSize
/(双)downloadFileItem.FileSize));

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; if(IsNetworkDisable)

                    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;打破;

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;等待outputStream.FlushAsync();

using (var inputStream = (await client.GetInputStreamAsync(contentUri).AsTask(cancellationToken.Token, downloadFileItem.Progress)).AsStreamForRead())
                    {
                        using (var outputStream = (await contentFile.OpenAsync(FileAccessMode.ReadWrite)).AsStreamForWrite())
                        {
                            var buffer = new byte[1024 * 1024];
                            int read;
                            downloadFileItem.Status = PDSConstants.DOWNLOAD_STATUS_PROGRESS_TEXT;
                            while ((read = await inputStream.ReadAsync(buffer, 0, buffer.Length)) > 0)/// code freeze here on no network
                            {
                                if (!IsDownloadToastAllreadyDisplayed)
                                {
                                    IsDownloadToastAllreadyDisplayed = true;
                                    if (!isRetryingDownload)
                                        PDSUtility.ShowToast("Panasonic Digital Signage", "Content downloading started");

                                }
                                outputStream.Seek(0, SeekOrigin.End);
                                await outputStream.WriteAsync(buffer, 0, read);
                                downloadFileItem.ProgressVisibility = Visibility.Visible;
                                downloadFileItem.Progress.BytesReceived += (ulong)read;
                                downloadFileItem.TotalDownloadedSize += read;
                                if (downloadFileItem.Progress.TotalBytesToReceive / 1024 > 1024)// if file size is greater than 1 mb.
                                    downloadFileItem.Status = String.Format("{0} of {1} mb. downloaded - {2}% complete.", Math.Round(((size + downloadFileItem.Progress.BytesReceived) / 1024F) / 1024F, 2), Math.Round(Convert.ToDecimal(((size + downloadFileItem.Progress.TotalBytesToReceive) / 1024F) / 1024F), 2), downloadFileItem.ProgressPercentage);
                                else
                                    downloadFileItem.Status = String.Format("{0} of {1} kb. downloaded - {2}% complete.", Math.Round((size + downloadFileItem.Progress.BytesReceived) / 1024F, 2), Math.Round(Convert.ToDecimal((size + downloadFileItem.Progress.TotalBytesToReceive) / 1024F), 2), downloadFileItem.ProgressPercentage);

                                downloadFileItem.ProgressPercentage = (int)(100 * ((double)downloadFileItem.TotalDownloadedSize / (double)downloadFileItem.FileSize));
                                if (IsNetworkDisable)
                                    break;
                            }
                            await outputStream.FlushAsync();

}

}

阿伦辛格拉瓦特|流动性| UWP | WPF Skype:arun.rawat017 |电子邮件:arun.rawat017@hotmail.com | arun.rawat017@gmail.com

Arun Singh Rawat | Mobility | UWP| WPF Skype: arun.rawat017 | Email:arun.rawat017@hotmail.com| arun.rawat017@gmail.com

推荐答案

通常,我们在开始下载任务之前检查网络,而不是在任务的过程中。我的建议是,在下载时,如果输入缓冲区为空, 去检查网络级别,看看是否有互联网可用。那么你可以根据结果做
做你想做的事情。 

Generally, we check the network before we start a download task instead of in the process of the task. My suggestion is that when downloading, if the input buffer is empty,  go to check the network level to see if internet is available. Then you could do things you want according to the result. 

你可以这样检查网络:

var connectionProfile = Windows.Networking.Connectivity.Networkinformation.GetInternetConnectionProfile();
if(connectionProfile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess)
{
   // get network
}




祝你好运,

Roy


这篇关于当下载期间设备丢失网络时,[uwp]流不会抛出错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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