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

    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. iOS - Swift Foundation 框架

    前言 框架是由许多类.方法.函数和文档按照一定的逻辑组织起来的集合,以使研发程序更容易. Foundation 框架:为所有程序开发奠定基础的框架称为 Foundation 框架. Cocoa :是指 ...

  2. 图文解说 Dijkstra.

    Dijkstra 太多文章了,有些简练,有些一笔带过.自己还是花了些时间才明白,刚脆自己写个图文说明的,希望能让还没明白的,尽快清楚. 问题:求某点到图中其他所有点的最短路径(权值和最小) Dijks ...

  3. mysql 聚集函数需要注意的问题

    1.当没有记录的时候,使用聚集函数,会导致出现一条记录,记录的取值都是NULL,如下:mysql> select name from student where name='David';Emp ...

  4. JavaWeb学习总结(九)--JDBC入门

    一.什么是JDBC JDBC(Java DataBase Connectivity)就是Java数据库连接,说白了就是用Java语言来操作数据库.原来我们操作数据库是在控制台使用SQL语句来操作数据库 ...

  5. 一次tomcat服务器被入侵解决办法

    突然tomcat目录下莫名其妙的多了几个war文件,里面内容只有一个index.jsp,打开控制台发现多了几个应用,我可以确定不是我部署上去的,顺着应用访问竟然看到了

  6. 如何查看与刷新DNS本地缓存

    如何查看与刷新DNS本地缓存 一.查看DNS本地缓存 在cmd窗口输入:ipconfig/displaydns 二.刷新DNS本地缓存 在cmd窗口输入:ipconfig/flushdns 之后输入: ...

  7. JS获取用户控件中的子控件Id

    用户控件 <asp:HiddenField ID="hfGradeId" runat="server" /> <asp:HiddenField ...

  8. LCA模板

    /*********--LCA模板--***************/ //设置好静态参数并构建好图的邻接表,然后调用lca_setquery()设置查询 //最后调用lca_start(),在lca ...

  9. OpenCV3编程入门笔记(6)自我验证各函数程序代码

    // asw.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <opencv2/opencv.hpp> usi ...

  10. mysql数据库回滚

    在应用$mysqli时,因没常用到数据回滚,老忘,整理下,做个记录. $mysqli->autocommit(FALSE);//自动提交设置关闭 $mysqli->query(" ...