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

    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);
            
        }

Threading.Tasks.Task多线程 静态全局变量(字典) --只为了记录的更多相关文章

  1. System.Threading.Tasks.Task 引起的 IIS 应用池崩溃

    接口服务运行一段时间后,IIS应用池就会突然挂掉,事件查看日志,会有事件日志Event ID为5011的错误 为应用程序池“PokeIn”提供服务的进程在与 Windows Process Activ ...

  2. System.Threading.Tasks.Task引起的IIS应用程序池崩溃

    问题现象 IIS应用程序池崩溃(Crash)的特征如下: 1. 从客户端看,浏览器一直处于连接状态,Web服务器无响应. 2. 从服务器端看(Windows Server 2008 + IIS 7.0 ...

  3. System.Threading.Tasks.Task 任务引起的IIS应用程序池崩溃

    转载:http://www.cnblogs.com/aaa6818162/p/4421305.html 问题现象 IIS应用程序池崩溃(Crash)的特征如下: 1. 从客户端看,浏览器一直处于连接状 ...

  4. threading.local()、多线程里全局变量锁

    这个人的系列文章值得一读:http://blog.51cto.com/suhaozhi/category3.html/p2,不过这个系列总共15偏,Python并发入门,有很多文字描述错误,有些道理也 ...

  5. 转载 Net多线程编程—System.Threading.Tasks.Parallel

    .Net多线程编程—System.Threading.Tasks.Parallel   System.Threading.Tasks.Parallel类提供了Parallel.Invoke,Paral ...

  6. .Net多线程编程—System.Threading.Tasks.Parallel

    System.Threading.Tasks.Parallel类提供了Parallel.Invoke,Parallel.For,Parallel.ForEach这三个静态方法. 1 Parallel. ...

  7. System.Threading.Tasks

    前言: 我们之前介绍了两种构建多线程软件的编程技术(使用异步委托或通过System.Threading的成员).这两个可以在任何版本的.NET平台工作. 关于System.Threading 的介绍 ...

  8. 静态局部变量、静态全局变量、extern全局变量、自动变量 札记

    静态局部变量 静态局部变量. 从称呼上我们可以看出,静态局部变量首先是一个局部变量,因此其只在定义它的函数内有效,冠以静态的头衔后,其生存期就被延长了,不会随着函数的返回而被撤销.我们可以这样来理解: ...

  9. Python:使用threading模块实现多线程编程

    转:http://blog.csdn.net/bravezhe/article/details/8585437 Python:使用threading模块实现多线程编程一[综述] Python这门解释性 ...

随机推荐

  1. javascript知识总汇

    命名: 变量名和函数命名:第一个单词小写以后每个单词首字母大写.geteElementById() 对象命名:每个单词首字母大写. 数据类型 typeof()方法返回数据类型. number数据类型: ...

  2. iOS - Swift NSData 数据

    前言 public class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding public class NSMutabl ...

  3. Spring 框架的设计理念与设计模式分析

    转载地址:https://www.ibm.com/developerworks/cn/java/j-lo-spring-principle/ Spring 作为现在最优秀的框架之一,已被广泛的使用,并 ...

  4. Ajax异步调用使用

    //验证通知号重复 function checkinformcodeagage() { var informcode = $("#txtinformcode").val(); if ...

  5. apiCloud中图片裁剪模块FNImageClip的使用

    思路 1.获取需裁剪图片的地址 2.跳转到裁剪页面 3.裁剪成功返回新图片地址 4.替换原有图片地址 增加修饰和事件 str += '<li class="tu image" ...

  6. 题目:在泛型为Integer的容器内添加一个字符串.

    这个题目有两种解法,第一种利用反射来解决: //ArrayList<Integer> list = new ArrayList<Integer>(); //在这个泛型为Inte ...

  7. mysql 命令行快速导出数据,导入数据

    如果数据有20几万以上的时候,下面的方法很实用 导出数据 1.into outfile select * from table into outfile 'C:/a.sql'; 2.mysqldump ...

  8. 如何在Mac OSX上安装xgboost

    听说xgboost效果很不错,于是准备学习下,但是发现大多数资料都是在讲如何在windows或linux下安装xgboost,而且照着官方文档也没有正确的安装好多线程的xgboost.最后还是从the ...

  9. asmca无法创建ASM磁盘

    现象 grid用户使用asmca无法创建asm磁盘,如下图 分析 如图所示,报错说是 Grid Infrastructure 出了问题.那么 Grid Infrastructure 是什么 ? 在安装 ...

  10. 静态库冲突的解决办法:duplicate symbol

    昨天在做微信sdk和xmpp的集成,发现各自单独集成没问题,一起集成却总报错,百度了好一会儿才知道,这应该是库冲突造成的问题 然后参考了很多文章,跟着敲敲一遍,却发现问题多多,最后主要综合结合了这两个 ...