none
backgrounddownloader 完成状态的如何检测才没有问题??????????? RRS feed

  • 问题

  • 我的程序里面在后台任务中下载了PDF,需要在下载完成时对SQLITE数据库做一些操作,在下载某些大一点的PDF时,完成后,发现被检查了两次,导致插入数据库报约束异常,请问怎么解决?

    代码如下:

      private async Task HandleDownloadAsync(DownloadOperation download, bool isNew)
            {
                cgtitle cg = new cgtitle();
                var pdffile = download.ResultFile;
                var cginfo = await cg.GetInfo(pdffile.Name.ToString());
                try
                {
                    // 将 DownloadOperation 附加到 TransferModel,以便下载进度可通知
                    TransferModel transfer = new TransferModel();
                    transfer.DownloadOperation = download;
                    transfer.Source = download.RequestedUri.ToString();
                    transfer.Destination = download.ResultFile.Path;
                    transfer.Progress = "0 / 0";
                    transfer.cg_title = cginfo.c_cg_title;
                    _transfers.Add(transfer);

                    // 当下载进度发生变化时的回调函数
                    Progress<DownloadOperation> progressCallback = new Progress<DownloadOperation>(DownloadProgress);

                    if (isNew)
                        await download.StartAsync().AsTask(App._cancelToken.Token, progressCallback); // 新增一个后台下载任务
                    else
                        await download.AttachAsync().AsTask(App._cancelToken.Token, progressCallback); // 监视已存在的后台下载任务

                    if (download.Progress.Status == BackgroundTransferStatus.Completed)
                    {

    //这里有时会被重复执行,如果不这样检查,那还有其它的检查方法有呢?????????????????????????

                        Debug.WriteLine(cginfo.i_generic_cg);

                         var pdffolder = await Utils.GetPdfFolder();
                        await pdffile.CopyAsync(pdffolder, pdffile.Name, NameCollisionOption.ReplaceExisting);
                        if (Common.Utils.IsNetworkAvailable())
                        {
                            try
                            {
                                await updateCgPdf(cginfo.i_generic_cg);
                            }
                            catch { }
                        }
                        showtip(cginfo.c_cg_title, "下载完成");
                    }
                    else
                    {
                        showtip(cginfo.c_cg_title, "下载失败");
                    }
                }
                catch (Exception ex)
                {
                    // 将异常转换为 WebErrorStatus 枚举,如果获取到的是 WebErrorStatus.Unknown 则说明此次异常不是涉及 web 的异常
                    WebErrorStatus error = BackgroundTransferError.GetStatus(ex.HResult);
                    showtip(cginfo.c_cg_title, "下载失败");
                }
                finally
                {
                    _transfers.Remove(_transfers.First(p => p.DownloadOperation == download));
                }
            }

    2015年1月27日 11:56

答案

  • 你好,

    建议参考官方的示例:https://code.msdn.microsoft.com/windowsapps/Background-Transfer-Sample-d7833f61

    它是为不同的项目创建 DownloadOperation, 然后调用DownloadOperation.StartAsync去下载

    // Once all downloads participating in the toast/tile update have been created, start them.
    Task[] downloadTasks = new Task[downloads.Length];
    for (int i = 0; i < downloads.Length; i++)
    {
                    downloadTasks[i] = DownloadAsync(downloads[i]);
    }
    
    await Task.WhenAll(downloadTasks);

    private async Task DownloadAsync(DownloadOperation download)
    {
                Log(String.Format(CultureInfo.CurrentCulture, "Downloading {0}", download.ResultFile.Name));
    
                try
                {
                    await download.StartAsync();
    
                    LogStatus(String.Format(CultureInfo.CurrentCulture, "Downloading {0} completed.", download.ResultFile.Name), NotifyType.StatusMessage);
                }
                catch (TaskCanceledException)
                {
                }
                catch (Exception ex)
                {
                    if (!IsExceptionHandled("Execution error", ex, download))
                    {
                        throw;
                    }
                }
    }

    可以尝试下,是否会出现你提到的多次检查问题


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年1月29日 4:53
    版主