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

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


后端代码:

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


线程处理类:

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

转载于:https://www.cnblogs.com/Echo529/p/6386490.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值