起因:因项目需要监控方法中计算进度,故而想通过AJAX调用,返回前端显示进度,结果开发中遇到第二个AJAX请求被挂起,需要等到第一个请求(计算)完成后,才会被处理到。
百度种种,知其原因是在Session上,但因项目过大,不敢随意删除Session。故而只能将后台计算方法放入线程,用委托的方式异步调用该计算方法,前端进行AJAX轮询,实时
检查计算进度。
轮询:setInterval 关闭clearInterval
委托Delegate的BeginInvoke是用于异步执行方法,Invoke是同步执行方法,EndInvoke会阻塞线程,直到方法执行完毕。
1 <script type="text/javascript"> 2 var si; 3 function GenerateSalary() { 4 $("#btnGenerateSalary").attr("disabled", "disabled"); 5 $("#btnGenerateSalary").text("生成中.."); 6 $("#btnGenerateSalary").hide(); 7 //$("#btnSeeProceess").show(); 8 $("#LoadingBg").show(); 9 $("#salaryLab").show(); 10 $.post("/AbnormalRoll/GenerateSalary", "date=" + $("#SalaryYear").val() + "-" + $("#SalaryMonth").val() + "&isNew=" + $("#chkIsNew").prop("checked"), function (d) { 11 si=setInterval(GetTotalNum, 1000); 12 }); 13 } 14 15 function GetTotalNum() { 16 $.post("/AbnormalRoll/GetTotalNum", "type=1", function (d) { 17 $("#totalNum").val(d.total); 18 $("#surplusNum").val(d.surplus); 19 if (d.total == d.surplus) { 20 clearInterval(si); 21 $("#LoadingBg").hide(); 22 $("#salaryLab").hide(); 23 $("#btnGenerateSalary").show(); 24 $("#btnGenerateSalary").text("生成工资"); 25 $("#btnGenerateSalary").removeAttr("disabled"); 26 } 27 }); 28 } 29 </script>
/// <summary> /// 生成工资信息 /// </summary> /// <returns>错误信息</returns> public ActionResult GenerateSalary(DateTime date, bool isNew) { string errmsg = string.Empty; GenerateSalaryDelegate method = new GenerateSalaryDelegate(GenerateSalaryMethod); IAsyncResult result = method.BeginInvoke(date, isNew, out errmsg, null, null); return Json(new { Result = true }); ////选中部门生成--遍历所有部门人员再生成 //try //{ // var salaryService = new GenerateSalaryService(); // salaryService.Clear(date); // salaryService.ClearGold(date); // var empfollows = BLL.EmpFollowBLL.GetEmpFollows(date.AddMonths(-1)); // var mamagerParam = new ManagerParam().FindOur(null).First(); // //因职务或部门要改变,所以不能用缓存中的人员 // var edoc = new BsonDocument().Ne("Name", "部门公共"); // var employees = new Employee().FindOur(edoc).ToList(); // var ml = employees.FirstOrDefault(emp => emp.Name == "马露"); // var i = 0; // var sm = employees.Where(a => a.Name == "苏敏").ToList(); // foreach (var emp in employees) // { // //if (emp.Name != "范美兰") // //{ // // continue; // //} // try // { // if (emp.QuitDate != null && Convert.ToDateTime(emp.QuitDate).FirstMontDay().Date >= DateTime.Now.FirstMontDay().Date) // { // DutyDayService.DelAbnormalAndArrange(date.FirstMontDay(), date.LastMonthDay(), emp); // } // salaryService = new GenerateSalaryService(emp.ToPartEmployee()); // //if (emp.Name == "苏敏")// || emp.Name == "张云虎" || emp.Name == "江浩") // salaryService.GenerateEmployeeSalary(date, isNew, empfollows, mamagerParam); // i++; // } // catch (Exception ex) // { // string errorMsg = string.Format("错误信息:{0} 人员ID:{1} 人员名称:{2}", ex.Message, emp.SId, // emp.Name); // continue; // return Json(new { Result = false, Message = errorMsg }); // } // } // return Json(new { Result = true }); //} //catch (Exception) //{ // return Json(new { Result = false, Message = "系统繁忙,请稍后再试" }); //} } public bool GenerateSalaryMethod(DateTime date, bool isNew, out string msg) { //选中部门生成--遍历所有部门人员再生成 try { var salaryService = new GenerateSalaryService(); salaryService.Clear(date); salaryService.ClearGold(date); msg = string.Empty; var empfollows = BLL.EmpFollowBLL.GetEmpFollows(date.AddMonths(-1)); var mamagerParam = new ManagerParam().FindOur(null).First(); //因职务或部门要改变,所以不能用缓存中的人员 var edoc = new BsonDocument().Ne("Name", "部门公共"); var employees = new Employee().FindOur(edoc).ToList(); var ml = employees.FirstOrDefault(emp => emp.Name == "马露"); var i = 0; var sm = employees.Where(a => a.Name == "苏敏").ToList(); TotalNum = employees.Count; foreach (var emp in employees) { //if (emp.Name != "范美兰") //{ // continue; //} try { if (emp.QuitDate != null && Convert.ToDateTime(emp.QuitDate).FirstMontDay().Date >= DateTime.Now.FirstMontDay().Date) { DutyDayService.DelAbnormalAndArrange(date.FirstMontDay(), date.LastMonthDay(), emp); } salaryService = new GenerateSalaryService(emp.ToPartEmployee()); //if (emp.Name == "苏敏")// || emp.Name == "张云虎" || emp.Name == "江浩") salaryService.GenerateEmployeeSalary(date, isNew, empfollows, mamagerParam); i++; Surplus = i; } catch (Exception ex) { string errorMsg = string.Format("错误信息:{0} 人员ID:{1} 人员名称:{2}", ex.Message, emp.SId, emp.Name); continue; msg = errorMsg; return false; //return Json(new { Result = false, Message = errorMsg }); } } return true; //return Json(new { Result = true }); } catch (Exception) { msg = "系统繁忙,请稍后再试"; return false; //return Json(new { Result = false, Message = "系统繁忙,请稍后再试" }); } } public delegate bool GenerateSalaryDelegate(DateTime date, bool isNew, out string msg); public ActionResult GetTotalNum(string type) { return Json(new { total = TotalNum, surplus = Surplus }); }
纠其原因,是因为Session引起的,在后来的工作中发现,只需要加上Session的会话状态即可解决上诉问题。
[SessionState(SessionStateBehavior.ReadOnly)]
原文地址:http://www.cnblogs.com/kissdodog/archive/2013/02/08/2909292.html