ASP.Net:Javascript 通过PageMethods 调用后端WebMethod方法 + 多线程数据处理 示例
http://blog.csdn.net/chz_cslg/article/details/7517751
背景:项目中有这样一个业务处理过程。1、上传一个文件; 2、上传完,读取该文件并对其里面大数据进行逐行多字段格式验证、并且做一些复杂的业务处理等。最终将处理后数据写入数据库中。一般做法:上传、读取、解析、验证、保存等操作一并完成。但因为文件数据量大,导致使用一般方法处理整个过程耗时太长,并且会出现超时页面不响应等情况。在同事的建议下,能否步骤处理(上传与后续所有操作分开处理),并使用多线程(读取、解析、验证、保存等操作)。思路有了,就等待实践验证了。在同事多方努力下,大功告成。时间上确实快很多,并且也不会看见浏览器一直等待的状况。
实际项目代码就不写出了,把其中思路整理成一个Demo,供观客参考使用。
铺垫有点长了,下面就贴代码了。
前端代码:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ScriptWebMethods._Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <script type="text/javascript">
- var clearSetInterval;
- function ProccessData() {
- document.body.style.cursor = "wait"; //让鼠标呈现沙漏状
- clearSetInterval = setInterval("getMessage()", 1000); //每隔一秒读取一次
- }
- function getMessage() {
- PageMethods.GetStatusMessage(showMessage); //掉用页面后端方法。 注意:这里方法调用及传参有点奇怪,是倒过来的。
- }
- function showMessage(statusValue) {
- if (statusValue == "success") {
- document.getElementById('divResult').innerHTML = statusValue;
- clearInterval(clearSetInterval); //清除计时
- document.body.style.cursor = "default"; //鼠标回到默认状
- }
- }
- </script>
- </head>
- <body style="height:600px;">
- <form id="form1" runat="server">
- <div>
- <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
- </asp:ScriptManager>
- <table style="width: 100%;">
- <tr>
- <td>
- <div id="divResult" style="border: 1px solid red; height: 30px; width: 100px; padding:4px;">
- </div>
- </td>
- </tr>
- <tr>
- <td>
- <asp:Button ID="btnProccessData" runat="server" Text="ProccessData" OnClick="btnProccessData_Click" />
- </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ScriptWebMethods._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript"> var clearSetInterval; function ProccessData() { document.body.style.cursor = "wait"; //让鼠标呈现沙漏状 clearSetInterval = setInterval("getMessage()", 1000); //每隔一秒读取一次 } function getMessage() { PageMethods.GetStatusMessage(showMessage); //掉用页面后端方法。 注意:这里方法调用及传参有点奇怪,是倒过来的。 } function showMessage(statusValue) { if (statusValue == "success") { document.getElementById('divResult').innerHTML = statusValue; clearInterval(clearSetInterval); //清除计时 document.body.style.cursor = "default"; //鼠标回到默认状 } } </script> </head> <body style="height:600px;"> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"> </asp:ScriptManager> <table style="width: 100%;"> <tr> <td> <div id="divResult" style="border: 1px solid red; height: 30px; width: 100px; padding:4px;"> </div> </td> </tr> <tr> <td> <asp:Button ID="btnProccessData" runat="server" Text="ProccessData" OnClick="btnProccessData_Click" /> </td> </tr> </table> </div> </form> </body> </html>
后端代码:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace ScriptWebMethods
- {
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- /// <summary>
- /// 提交处理
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void btnProccessData_Click(object sender, EventArgs e)
- {
- ProccessThread thread = new ProccessThread();
- thread.State = "false";
- thread.RunProccess();
- Session["ProccessThread"] = thread; //把线程处理实例塞给Session,保证数据同步
- ClientScript.RegisterStartupScript(this.GetType(), "RunProccess", "ProccessData();", true);
- }
- [System.Web.Services.WebMethod]
- public static string GetStatusMessage()
- {
- //从Session取出线程中实时状态数据判断是否处理完成
- ProccessThread thread = HttpContext.Current.Session["ProccessThread"] as ProccessThread;
- return thread.State;
- }
- }
- }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ScriptWebMethods { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } /// <summary> /// 提交处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnProccessData_Click(object sender, EventArgs e) { ProccessThread thread = new ProccessThread(); thread.State = "false"; thread.RunProccess(); Session["ProccessThread"] = thread; //把线程处理实例塞给Session,保证数据同步 ClientScript.RegisterStartupScript(this.GetType(), "RunProccess", "ProccessData();", true); } [System.Web.Services.WebMethod] public static string GetStatusMessage() { //从Session取出线程中实时状态数据判断是否处理完成 ProccessThread thread = HttpContext.Current.Session["ProccessThread"] as ProccessThread; return thread.State; } } }
线程处理类:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Threading;
- namespace ScriptWebMethods
- {
- public class ProccessThread
- {
- public string State { set; get;}
- /// <summary>
- ///
- /// </summary>
- public void RunProccess()
- {
- State = "false";
- Thread thread = new Thread(ProccessData); //启动一个线程处理
- thread.Start();
- }
- /// <summary>
- /// 处理数据
- /// </summary>
- private void ProccessData()
- {
- //这里你可以做繁杂的业务数据处理:读取文件处理
- for (int i = 0; i < (100000000 + 1); i++)
- {
- if (i == 1000000)
- {
- State = "success";
- }
- }
- }
- }
- }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Threading; namespace ScriptWebMethods { public class ProccessThread { public string State { set; get;} /// <summary> /// /// </summary> public void RunProccess() { State = "false"; Thread thread = new Thread(ProccessData); //启动一个线程处理 thread.Start(); } /// <summary> /// 处理数据 /// </summary> private void ProccessData() { //这里你可以做繁杂的业务数据处理:读取文件处理 for (int i = 0; i < (100000000 + 1); i++) { if (i == 1000000) { State = "success"; } } } } }
如果过程中遇到“PageMethods 未定义错误”,可能导致原因:
1、前端页面ScriptManager 属性EnablePageMethods一定要设置成 true;
2、后端WebMethod方法设成:public static;
3、后端WebMethod方法加特性:[System.Web.Services.WebMethod];
4、检查web.config中是否加入对于asp.NET ajax的支持的代码。
清空回声