首页 > 代码库 > Threading.Tasks.Task多线程 静态全局变量(字典) --只为了记录

Threading.Tasks.Task多线程 静态全局变量(字典) --只为了记录

      

--------------------------------------------------------------后台代码------------------------------------------

    public JsonResult ImportPDF(Int64 id)
        {
            try
            {
                Guid currentGuid = Guid.NewGuid();

                if (Request.Files["FileData"].HasFile())
                {
                    HttpPostedFileBase file = Request.Files["FileData"];
                    //if (file.InputStream.Length > 16*1024*1024)
                    //{
                    //    throw new Exception("文件过大,导入不成功!");
                    //}
                    CreateFolder();
                    string path = Server.MapPath("/Ebook");
                    string fileName = "1.pdf";

                    //Directory.CreateDirectory(path);
                    file.SaveAs(string.Format(@"{0}\{1}", path, fileName));
                    PDFText(string.Format(@"{0}\{1}", path, fileName), id, currentGuid);
                }

                return Json(currentGuid.ToString(), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
        }

        /// <summary>
        /// 获取导入PDF的进度
        /// </summary>
        /// <returns></returns>
        public JsonResult GetPdfProgress(string guidStr)
        {
            try
            {
                Guid guid = new Guid(guidStr.Trim(‘"‘));
                if (pdfProDic.ContainsKey(guid))
                {
                    return Json(new { guidKey = guidStr, proVal = pdfProDic[guid] }, JsonRequestBehavior.AllowGet);
                }
                return Json(new {guidKey = guidStr}, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        
        //注意要使用静态(字典与GUID为了应对多人同时访问)
        static Dictionary<Guid,int> pdfProDic=new Dictionary<Guid, int>();
        
        public void PDFText(string fileName, Int64 id,Guid guid)
        {
            System.Threading.Tasks.Task.Factory.StartNew(user =>
            {
                try
                {
                    pdfProDic.Add(guid,0);
                    Domain.UserModel.User currentUser = user as Domain.UserModel.User;
                    if (user != null)
                    {
                        #region 执行pdf导入数据库
                        //注意加载PDF文件过大会出错
                        PDDocument doc = PDDocument.load(fileName);
                        PDFTextStripper pdfStripper = new PDFTextStripper();


                        short currentPage = GetMaxPageNumber(id);

                        if (currentPage < 10000)
                            currentPage = 10000;

                        float j = 0;
                        int progress = 0;
                        for (int i = 0; i < doc.getNumberOfPages(); i++)
                        {
                            currentPage++;

                            //索引是从0开始,第一页表示0~1
                            pdfStripper.setStartPage(i);
                            pdfStripper.setEndPage(i + 1);
                            String pdfStr = pdfStripper.getText(doc);

                            var target = EntAppFrameWorkContext.Application.ExtenedT<Ebook, Int64, EbookAppExt>().
                                CreatePage(
                                    id,
                                    currentPage,
                                    pdfStr,
                                    currentUser);
                            j = i+1;
                            progress = (int)((j / doc.getNumberOfPages()) * 100);
                            if (Convert.ToInt32(j) >= doc.getNumberOfPages())
                            {
                                progress = 100;
                                //为了性能的提升,这时候进行排序
                                EntAppFrameWorkContext.Application.ExtenedT<Ebook,Int64,EbookAppExt>().InitEbookPage(id);
                            }
                            pdfProDic[guid] = progress;
                        }
                        doc.close();
                        #endregion
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                
            }, base.CurrentUser);
            
        }