ASP.Net:Javascript 通过PageMethods 调用后端WebMethod方法 + 多线程数据处理 示例

ASP.Net:Javascript 通过PageMethods 调用后端WebMethod方法 + 多线程数据处理 示例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chz_cslg/article/details/7517751

        背景:项目中有这样一个业务处理过程。1、上传一个文件; 2、上传完,读取该文件并对其里面大数据进行逐行多字段格式验证、并且做一些复杂的业务处理等。最终将处理后数据写入数据库中。一般做法:上传、读取、解析、验证、保存等操作一并完成。但因为文件数据量大,导致使用一般方法处理整个过程耗时太长,并且会出现超时页面不响应等情况。在同事的建议下,能否步骤处理(上传与后续所有操作分开处理),并使用多线程(读取、解析、验证、保存等操作)。思路有了,就等待实践验证了。在同事多方努力下,大功告成。时间上确实快很多,并且也不会看见浏览器一直等待的状况。

        实际项目代码就不写出了,把其中思路整理成一个Demo,供观客参考使用。

        铺垫有点长了,下面就贴代码了。

前端代码:

  1.  
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ScriptWebMethods._Default" %>
  2.  
     
  3.  
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
    <html xmlns="http://www.w3.org/1999/xhtml">
  5.  
    <head runat="server">
  6.  
    <title></title>
  7.  
     
  8.  
    <script type="text/javascript">
  9.  
    var clearSetInterval;
  10.  
    function ProccessData() {
  11.  
    document.body.style.cursor = "wait"; //让鼠标呈现沙漏状
  12.  
    clearSetInterval = setInterval("getMessage()", 1000); //每隔一秒读取一次
  13.  
    }
  14.  
     
  15.  
    function getMessage() {
  16.  
    PageMethods.GetStatusMessage(showMessage); //掉用页面后端方法。 注意:这里方法调用及传参有点奇怪,是倒过来的。
  17.  
    }
  18.  
     
  19.  
    function showMessage(statusValue) {
  20.  
    if (statusValue == "success") {
  21.  
    document.getElementById('divResult').innerHTML = statusValue;
  22.  
    clearInterval(clearSetInterval); //清除计时
  23.  
    document.body.style.cursor = "default"; //鼠标回到默认状
  24.  
    }
  25.  
    }
  26.  
     
  27.  
    </script>
  28.  
     
  29.  
    </head>
  30.  
    <body style="height:600px;">
  31.  
    <form id="form1" runat="server">
  32.  
    <div>
  33.  
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
  34.  
    </asp:ScriptManager>
  35.  
    <table style=" 100%;">
  36.  
    <tr>
  37.  
    <td>
  38.  
    <div id="divResult" style="border: 1px solid red; height: 30px; 100px; padding:4px;">
  39.  
    </div>
  40.  
    </td>
  41.  
    </tr>
  42.  
    <tr>
  43.  
    <td>
  44.  
    <asp:Button ID="btnProccessData" runat="server" Text="ProccessData" OnClick="btnProccessData_Click" />
  45.  
    </td>
  46.  
    </tr>
  47.  
    </table>
  48.  
    </div>
  49.  
    </form>
  50.  
    </body>
  51.  
    </html>


后端代码:

  1.  
    using System;
  2.  
    using System.Collections.Generic;
  3.  
    using System.Linq;
  4.  
    using System.Web;
  5.  
    using System.Web.UI;
  6.  
    using System.Web.UI.WebControls;
  7.  
     
  8.  
    namespace ScriptWebMethods
  9.  
    {
  10.  
    public partial class _Default : System.Web.UI.Page
  11.  
    {
  12.  
    protected void Page_Load(object sender, EventArgs e)
  13.  
    {
  14.  
     
  15.  
    }
  16.  
     
  17.  
    /// <summary>
  18.  
    /// 提交处理
  19.  
    /// </summary>
  20.  
    /// <param name="sender"></param>
  21.  
    /// <param name="e"></param>
  22.  
    protected void btnProccessData_Click(object sender, EventArgs e)
  23.  
    {
  24.  
    ProccessThread thread = new ProccessThread();
  25.  
    thread.State = "false";
  26.  
    thread.RunProccess();
  27.  
     
  28.  
    Session["ProccessThread"] = thread; //把线程处理实例塞给Session,保证数据同步
  29.  
    ClientScript.RegisterStartupScript(this.GetType(), "RunProccess", "ProccessData();", true);
  30.  
    }
  31.  
     
  32.  
     
  33.  
    [System.Web.Services.WebMethod]
  34.  
    public static string GetStatusMessage()
  35.  
    {
  36.  
    //从Session取出线程中实时状态数据判断是否处理完成
  37.  
    ProccessThread thread = HttpContext.Current.Session["ProccessThread"] as ProccessThread;
  38.  
    return thread.State;
  39.  
    }
  40.  
    }
  41.  
    }


线程处理类:

  1.  
    using System;
  2.  
    using System.Collections.Generic;
  3.  
    using System.Linq;
  4.  
    using System.Web;
  5.  
    using System.Threading;
  6.  
     
  7.  
    namespace ScriptWebMethods
  8.  
    {
  9.  
    public class ProccessThread
  10.  
    {
  11.  
    public string State { set; get;}
  12.  
     
  13.  
    /// <summary>
  14.  
    ///
  15.  
    /// </summary>
  16.  
    public void RunProccess()
  17.  
    {
  18.  
    State = "false";
  19.  
    Thread thread = new Thread(ProccessData); //启动一个线程处理
  20.  
    thread.Start();
  21.  
    }
  22.  
     
  23.  
    /// <summary>
  24.  
    /// 处理数据
  25.  
    /// </summary>
  26.  
    private void ProccessData()
  27.  
    {
  28.  
    //这里你可以做繁杂的业务数据处理:读取文件处理
  29.  
    for (int i = 0; i < (100000000 + 1); i++)
  30.  
    {
  31.  
    if (i == 1000000)
  32.  
    {
  33.  
    State = "success";
  34.  
    }
  35.  
    }
  36.  
    }
  37.  
    }
  38.  
    }


如果过程中遇到“PageMethods 未定义错误”,可能导致原因:

1、前端页面ScriptManager 属性EnablePageMethods一定要设置成 true;

2、后端WebMethod方法设成:public static;

3、后端WebMethod方法加特性:[System.Web.Services.WebMethod];

4、检查web.config中是否加入对于asp.net ajax的支持的代码。

望对您有所帮助。O(∩_∩)O~

Kevin.Chen

2012-04-27  于苏州太仓

原文地址:https://www.cnblogs.com/Jeely/p/10772407.html