Browse Source

合并- 修复一次加入较多加密文件,加密弹窗线程异常问题

ZhouJieSheng 1 year ago
parent
commit
15549f2b74
1 changed files with 122 additions and 122 deletions
  1. 122 122
      PDF Office/ViewModels/Dialog/ToolsDialogs/MergeDialogViewModel.cs

+ 122 - 122
PDF Office/ViewModels/Dialog/ToolsDialogs/MergeDialogViewModel.cs

@@ -550,168 +550,168 @@ namespace PDF_Master.ViewModels.Dialog.ToolsDialogs
             }
             bool showDialog = false;
             ProcessVisible = Visibility.Visible;
-            MaxValue = FilePath.Length;
-            await System.Threading.Tasks.Task.Run((() =>
+            MaxValue = FilePath.Length;
+            for (int i = 0; i < FilePath.Length; i++)
             {
-                for (int i = 0; i < FilePath.Length; i++)
+                //加入异步语句,防止UI卡死,显示加载进度条
+                await System.Threading.Tasks.Task.Delay(1);
+                //如果中断了文件添加,则取消循环
+                if (CancelAddFiles)
                 {
-                    //如果中断了文件添加,则取消循环
-                    if (CancelAddFiles)
+                    break;
+                }
+                MergeObject mergeObject = new MergeObject();
+                mergeObject.FilePath = FilePath[i];
+
+                //通过路径判断文件是否已添加
+                bool IsExists = false;
+                for (int j = 0; j < MergeObjectlist.Count; j++)
+                {
+                    if (MergeObjectlist[j].FilePath == mergeObject.FilePath)
                     {
-                        break;
+                        IsExists = true;
                     }
-                    MergeObject mergeObject = new MergeObject();
-                    mergeObject.FilePath = FilePath[i];
+                }
+                if (IsExists)
+                {
+                    continue;
+                }
+                string FileType = Path.GetExtension(mergeObject.FilePath).Trim().ToLower();
+                if (string.IsNullOrEmpty(FileType))
+                {
+                    showDialog = true;
+                    //获取不到文件类型
+                    continue;
+                }
+                if (FileType != ".pdf")
+                {
+                    string imagetype = "*" + FileType;
+                    string[] x = Properties.Resources.imageex.ToLower().Split(';');
+                    List<string> list = x.ToList();
+                    int imageindex = list.IndexOf(imagetype);
+                    if (imageindex < 0)
+                    {
+                        showDialog = true;
+                        //图片格式不支持
+                        continue;
+                    };
 
-                    //通过路径判断文件是否已添加
-                    bool IsExists = false;
-                    for (int j = 0; j < MergeObjectlist.Count; j++)
+                    mergeObject.DocName = Path.GetFileName(mergeObject.FilePath);
+                    mergeObject.DocPageCount = 1 + " " + App.MainPageLoader.GetString("Merge_ItemPages");
+                    mergeObject.SDKPageCount = 1;
+                    mergeObject.DocSize = CommonHelper.GetFileSize(mergeObject.FilePath);
+                    try
                     {
-                        if (MergeObjectlist[j].FilePath == mergeObject.FilePath)
-                        {
-                            IsExists = true;
-                        }
+                        mergeObject.DocThumbnail = new BitmapImage(new Uri(mergeObject.FilePath));
                     }
-                    if (IsExists)
+                    catch (Exception)
                     {
+                        showDialog = true;
+                        //解码错误
                         continue;
                     }
-                    string FileType = Path.GetExtension(mergeObject.FilePath).Trim().ToLower();
-                    if (string.IsNullOrEmpty(FileType))
+                }
+                else
+                {
+                    CPDFDocument doc = CPDFDocument.InitWithFilePath(mergeObject.FilePath);
+                    if (doc == null)
                     {
                         showDialog = true;
-                        //获取不到文件类型
+                        //PDF打开失败
                         continue;
                     }
-                    if (FileType != ".pdf")
+
+                    ///Fix:
+                    ///情况分为:
+                    ///开启路径是当前路径:
+                    ///已解锁或本身就无密码->直接加入
+                    ///未解锁-> release
+                    ///
+                    /// 不是当前路径
+                    ///解锁或取消->
+                    ///
+
+                    if (CurrentFilePath == doc.FilePath)
                     {
-                        string imagetype = "*" + FileType;
-                        string[] x = Properties.Resources.imageex.ToLower().Split(';');
-                        List<string> list = x.ToList();
-                        int imageindex = list.IndexOf(imagetype);
-                        if (imageindex < 0)
+                        if ((!(!doc.IsLocked && (SecurityHelper.CheckHaveAllPermissions(doc)))) &&
+                            (!(!string.IsNullOrEmpty(currentLoadedPassword) &&
+                            doc.UnlockWithPassword(currentLoadedPassword) &&
+                            (doc.CheckOwnerPassword(currentLoadedPassword) || SecurityHelper.CheckHaveAllPermissions(doc)))))
                         {
-                            showDialog = true;
-                            //图片格式不支持
+                            doc.Release();
                             continue;
-                        };
-
-                        mergeObject.DocName = Path.GetFileName(mergeObject.FilePath);
-                        mergeObject.DocPageCount = 1 + " " + App.MainPageLoader.GetString("Merge_ItemPages");
-                        mergeObject.SDKPageCount = 1;
-                        mergeObject.DocSize = CommonHelper.GetFileSize(mergeObject.FilePath);
-                        try
-                        {
-                            mergeObject.DocThumbnail = new BitmapImage(new Uri(mergeObject.FilePath));
                         }
-                        catch (Exception)
+                        //添加的第一个文档有权限密码时,保存密码,避免合并时因没有密码导致合并失败
+                        //表现为在首页打开一个带权限的文档进入合并,点击合并按钮没有反应
+                        if (!string.IsNullOrEmpty(currentLoadedPassword))
                         {
-                            showDialog = true;
-                            //解码错误
-                            continue;
+                            mergeObject.Password = currentLoadedPassword;
                         }
                     }
                     else
                     {
-                        CPDFDocument doc = CPDFDocument.InitWithFilePath(mergeObject.FilePath);
-                        if (doc == null)
-                        {
-                            showDialog = true;
-                            //PDF打开失败
-                            continue;
-                        }
-
-                        ///Fix:
-                        ///情况分为:
-                        ///开启路径是当前路径:
-                        ///已解锁或本身就无密码->直接加入
-                        ///未解锁-> release
-                        ///
-                        /// 不是当前路径
-                        ///解锁或取消->
-                        ///
-
-                        if (CurrentFilePath == doc.FilePath)
-                        {
 
-                            if ((!(!doc.IsLocked && (SecurityHelper.CheckHaveAllPermissions(doc)))) &&
-                                (!(!string.IsNullOrEmpty(currentLoadedPassword) &&
-                                doc.UnlockWithPassword(currentLoadedPassword) &&
-                                (doc.CheckOwnerPassword(currentLoadedPassword) || SecurityHelper.CheckHaveAllPermissions(doc)))))
-                            {
-                                doc.Release();
-                                continue;
-                            }
-                            //添加的第一个文档有权限密码时,保存密码,避免合并时因没有密码导致合并失败
-                            //表现为在首页打开一个带权限的文档进入合并,点击合并按钮没有反应
-                            if (!string.IsNullOrEmpty(currentLoadedPassword))
-                            {
-                                mergeObject.Password = currentLoadedPassword;
-                            }
-                        }
-                        else
+                        VerifyPasswordResult condition = new VerifyPasswordResult();
+                        App.Current.Dispatcher.Invoke(() =>
                         {
-                            VerifyPasswordResult condition = new VerifyPasswordResult();
                             //切换一下UI线程,避免解密弹窗异常
-                            App.Current.Dispatcher.Invoke(() =>
-                            {
-                                condition = SecurityHelper.VerifyPasswordByPasswordKind(doc, EnumPasswordKind.StatusPermissionsPassword, dialogs);
-                            });
-                            if (condition.IsDiscryptied)
+                            condition = SecurityHelper.VerifyPasswordByPasswordKind(doc, EnumPasswordKind.StatusPermissionsPassword, dialogs);
+                        });
+                        if (condition.IsDiscryptied)
+                        {
+                            if (condition.Password != null)
                             {
-                                if (condition.Password != null)
+                                mergeObject.Password = condition.Password;
+                                if (doc.UnlockWithPassword(condition.Password) && doc.CheckOwnerPassword(condition.Password))
                                 {
-                                    mergeObject.Password = condition.Password;
-                                    if (doc.UnlockWithPassword(condition.Password) && doc.CheckOwnerPassword(condition.Password))
-                                    {
 
-                                    }
                                 }
                             }
-                            else
-                            {
-                                doc.Release();
-                                continue;
-                            }
-
-                        }
-
-                        mergeObject.DocName = doc.FileName;
-
-                        if (doc.PageCount > 1)
-                        {
-                            mergeObject.DocPageCount = doc.PageCount.ToString() + " " + App.MainPageLoader.GetString("Merge_ItemPages");
-                            mergeObject.IsEvenPageIsEnabled = true;
                         }
                         else
                         {
-                            mergeObject.DocPageCount = doc.PageCount.ToString() + " " + App.MainPageLoader.GetString("Merge_ItemPage");
-                            mergeObject.IsEvenPageIsEnabled = false;
+                            doc.Release();
+                            continue;
                         }
-                        mergeObject.SDKPageCount = doc.PageCount;
-                        mergeObject.DocSize = CommonHelper.GetFileSize(mergeObject.FilePath);
 
-                        //获取第一页缩略图
-                        CPDFPage page = doc.PageAtIndex(0);
-                        Size size = doc.GetPageSize(0);
 
-                        byte[] bmpData = new byte[(int)(size.Width * size.Height * 4)];
-                        WriteableBitmap WirteBitmap = new WriteableBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Bgra32, null);
-                        page.RenderPageBitmap(0, 0, (int)size.Width, (int)size.Height, 0xFFFFFFFF, bmpData, 1);
-                        WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)size.Width, (int)size.Height), bmpData, WirteBitmap.BackBufferStride, 0);
-                        WirteBitmap.Freeze();
-                        mergeObject.DocThumbnail = WirteBitmap;
+                    }
+
+                    mergeObject.DocName = doc.FileName;
 
-                        doc.Release();
+                    if (doc.PageCount > 1)
+                    {
+                        mergeObject.DocPageCount = doc.PageCount.ToString() + " " + App.MainPageLoader.GetString("Merge_ItemPages");
+                        mergeObject.IsEvenPageIsEnabled = true;
                     }
-                    App.Current.Dispatcher.Invoke(() =>
+                    else
                     {
-                        MergeObjectlist.Add(mergeObject);
-                    });
-                    CurrentValue = i;
-                    UpDataMergeObjectIndex();
-                }
-            }));
+                        mergeObject.DocPageCount = doc.PageCount.ToString() + " " + App.MainPageLoader.GetString("Merge_ItemPage");
+                        mergeObject.IsEvenPageIsEnabled = false;
+                    }
+                    mergeObject.SDKPageCount = doc.PageCount;
+                    mergeObject.DocSize = CommonHelper.GetFileSize(mergeObject.FilePath);
+
+                    //获取第一页缩略图
+                    CPDFPage page = doc.PageAtIndex(0);
+                    Size size = doc.GetPageSize(0);
+
+                    byte[] bmpData = new byte[(int)(size.Width * size.Height * 4)];
+                    WriteableBitmap WirteBitmap = new WriteableBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Bgra32, null);
+                    page.RenderPageBitmap(0, 0, (int)size.Width, (int)size.Height, 0xFFFFFFFF, bmpData, 1);
+                    WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)size.Width, (int)size.Height), bmpData, WirteBitmap.BackBufferStride, 0);
+                    WirteBitmap.Freeze();
+                    mergeObject.DocThumbnail = WirteBitmap;
+
+                    doc.Release();
+                }
+                App.Current.Dispatcher.Invoke(() =>
+                {
+                    MergeObjectlist.Add(mergeObject);
+                });
+                CurrentValue = i;
+                UpDataMergeObjectIndex();
+            }
             ProcessVisible = Visibility.Collapsed;
             if (showDialog)
             {