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

时间:2023-03-08 17:35:13

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

2012年04月27日 16:59:16 奋斗的小壁虎 阅读数:4500
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.****.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>

后端代码:

  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. }

线程处理类:

  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. }

如果过程中遇到“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  于苏州太仓