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

时间: 2024-08-06 15:55:36

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

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

问题现象 IIS应用程序池崩溃(Crash)的特征如下: 1. 从客户端看,浏览器一直处于连接状态,Web服务器无响应. 2. 从服务器端看(Windows Server 2008 + IIS 7.0),在事件日志中会出现Event ID为5010的错误: A process serving application pool 'q.cnblogs.com' failed to respond to a ping. The process id was '20080'. 这个错误的意思是:IIS检

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

接口服务运行一段时间后,IIS应用池就会突然挂掉,事件查看日志,会有事件日志Event ID为5011的错误 为应用程序池"PokeIn"提供服务的进程在与 Windows Process Activation Service 通信时出现严重错误.该进程 ID 为"2268".数据字段包含错误号. 最后直接程序池直接被禁用 应用程序池"PokeIn"将被自动禁用,原因是为此应用程序池提供服务的进程中出现一系列错误. 查看管理事件 Applicat

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

转载:http://www.cnblogs.com/aaa6818162/p/4421305.html 问题现象 IIS应用程序池崩溃(Crash)的特征如下: 1. 从客户端看,浏览器一直处于连接状态,Web服务器无响应. 2. 从服务器端看(Windows Server 2008 + IIS 7.0),在事件日志中会出现Event ID为5010的错误: A process serving application pool 'q.cnblogs.com' failed to respond

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

System.Threading.Tasks.Parallel类提供了Parallel.Invoke,Parallel.For,Parallel.ForEach这三个静态方法. 1 Parallel.Invoke 尽可能并行执行所提供的每个操作,除非用户取消了操作. 方法: 1)public static void Invoke(params Action[] actions); 2)public static void Invoke(ParallelOptions parallelOption

Python:使用threading模块实现多线程(转)

分类: python   标签: thread    评论: 暂无评论   阅读:5,420 views 综述 Python这门解释性语言也有专门的线程模型,Python虚拟机使用GIL(Global Interpreter Lock,全局解释器锁)来互斥线程对共享资源的访问,但暂时无法利用多处理器的优势. 在Python中我们主要是通过thread和 threading这两个模块来实现的,其中Python的threading模块是对thread做了一些包装的,可以更加方便的被使用,所以我们使用

c# Task多线程并行任务中等待所有线程都执行完成

C#多线程中如何等待所有线程的任务都执行完成呢?在.net 4.0以4.0+中,有Task.WaitAll(params Task[] tasks)方法来等待所有Task[],而不需要更多的操作.按照微软官方的文档,我写了一个实例来调试和说明: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Thre

C/C++语言变量问题(全局变量、局部变量、静态全局变量、静态局部变量)

1.C/C++变量根据定义的位置的不同的生命周期,具有不同的作用域,作用域可分为6种: 全局作用域,局部作用域,语句作用域,类作用域,命名空间作用域和文件作用域. 从作用域看: 1>全局变量具有全局作用域.全局变量只需在一个源文件中定义,就可以作用于所有的源文件.当然,其他不包含全局变量定义的源文件需要用extern 关键字再次声明这个全局变量. 2>静态局部变量具有局部作用域,它只被初始化一次,自从第一次被初始化直到程序运行结束都一直存在,它和全局变量的区别在于全局变量对所有的函数都是可见的

全局变量、局部变量、静态全局变量、静态局部变量在内存里的区别

一.程序的内存分配 一个由C/C++编译的程序占用的内存分为以下几个部分: 1.栈区(stack)- 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构中的栈. 2.堆区(heap) - 一般由程序员分配释放, 若程序员不释放,程序结束时可能由OS回收 .注意它与数据结构中的堆是两回事,分配方式倒是类似于链表. 3.全局区(静态区)(static)- 全局变量和静态变量的存储是放在一块的,初始化的全局变量和静态变量在一块区域, 未初始化的全局变量和未初始化的静态变

Threading.Tasks 简单的使用

using Lemon.Common; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Timers; using System.Web; namespace Lemon.Stats.Web.Service { public class SynchCacheDataService { /// <summary> /// 定时