C# ASP 异步存储数据

Posted 惊涛拍岸,风卷残云,指端似有雄兵百万

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# ASP 异步存储数据相关的知识,希望对你有一定的参考价值。

1.异步委托

在导航栏接收到提交的请求后,调用个各子画面的保存答案方法,之后实例化委托 saveToDB 。

当执行BeginInvoke后,服务器会另起线程执行saveToDB里的的方法,因为这里要保存的数据较多,所以要呈现下一个页面,

也就是:Response.Redirect("~/Default.aspx");调用后,画面虽然迁移了,后台还在执行saveToDB里的方法

  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 using System.Threading;
  8 using System.Threading.Tasks;
  9 using System.Web.UI.htmlControls;
 10 using System.Drawing;
 11 
 12 public partial class SiteMaster : System.Web.UI.MasterPage
 13 {
 14     public delegate void ThreadHandler();//异步委托
 15     BasePage currentPage = null;
 16     protected void Page_Load(object sender, EventArgs e)
 17     {
 18         string strUrl = Request.Url.ToString();
 19 
 20         if (strUrl.Contains("Questionnaire"))//只有在答题页面
 21         {
 22             currentPage = Page as BasePage;
 23         }
 24     }
 25 
 26     // 导航栏事件
 27     protected void MenuMaster_MenuItemClick(object sender, MenuEventArgs e)
 28     {
 29         string strUrl = Request.Url.ToString();
 30 
 31         Menu menu = (Menu)sender;
 32         //提交问卷,所有答案入库
 33         if (menu.SelectedValue == "SubmitAns")
 34         {
 35             if (strUrl.Contains("Questionnaire"))//只有在答题页面
 36             {
 37                 int position_1 = strUrl.LastIndexOf("Questionnaire") + 13;
 38                 int position_2 = strUrl.LastIndexOf(".");
 39                 string strCurrentNum = strUrl.Substring(position_1, position_2 - position_1);
 40                 int num = int.Parse(strCurrentNum);
 41 
 42                 if (checkInput())//输入Check
 43                 {
 44                     return;
 45                 }
 46                 saveAnswer(num, true, false);
 47 
 48                 AsyncCallback callback = new AsyncCallback(Callback); //回调函数声明
 49                 ThreadHandler ad = new ThreadHandler(saveToDB);//实例化委托类型
 50                 IAsyncResult ar = ad.BeginInvoke(callback, ad);//开始调用
 51                 
 52             }
 53             Response.Redirect("~/Default.aspx");
 54         }
 55     }
 56 
 57     private bool checkInput()
 58     {
 59         return currentPage.checkInput();
 60     }
 61 
 62     private bool saveAnswer(int num, bool isSubmit, bool isNext)
 63     {
 64         if (num == 1 && !isSubmit && !isNext)
 65         {
 66             currentPage.saveAnswer();
 67             Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert(‘已经是第一道题‘);</script>");
 68             return false;
 69         }
 70         else if (num == 35 && !isSubmit && isNext)
 71         {
 72             currentPage.saveAnswer();
 73             Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert(‘已经是最后一道题‘);</script>");
 74             return false;
 75         }
 76         else if (num == 30)
 77         {
 78             String className = (String)Request.Form.Get("className");
 79             String authorName = (String)Request.Form.Get("authorName");
 80             currentPage.saveAnswer(className, authorName);
 81         }
 82         else if (num == 31)
 83         {
 84             String projectName = (String)Request.Form.Get("projectName");
 85             String DropDownList1 = (String)Request.Form.Get("ctl00$MainContent$DropDownList1");
 86             String DropDownList2 = (String)Request.Form.Get("ctl00$MainContent$DropDownList2");
 87             currentPage.saveAnswer(projectName, DropDownList1, DropDownList2);
 88         }
 89         else if (num == 32)
 90         {
 91             String Ctime = (String)Request.Form.Get("Ctime");
 92             String Cname = (String)Request.Form.Get("Cname");
 93             currentPage.saveAnswer(Ctime, Cname);
 94         }
 95         else
 96         {
 97             currentPage.saveAnswer();
 98         }
 99         return true;
100     }
101 
102     private void saveToDB()
103     {
104         UserInfo user = (UserInfo)Session["UserInfo"];
105         Answer answer = (Answer)Session["Answer"];
106         Dao dao = new Dao();//所有答案入库
107         lock (answer)
108         {
109             //Thread.Sleep(10000);
110             if (answer != null)
111             {
112                 answer.Userid = user.Pkid;
113                 if (user.Status == "0")
114                 {
115                     dao.saveAnswer(answer);
116                 }
117                 else
118                 {
119                     dao.updateAnswer(answer);
120                 }
121                 dao.updateUser(user.Pkid.ToString(), "1");
122                 dao.ConvertToView(answer);
123             }
124         }
125     }
126 
127     /// <summary>
128     /// 回掉函数
129     /// </summary>
130     /// <param name="ar"></param>
131     void Callback(IAsyncResult ar)
132     {
133         ThreadHandler andl = (ThreadHandler)ar.AsyncState;
134         andl.EndInvoke(ar);
135     }
136 }

 

以上是关于C# ASP 异步存储数据的主要内容,如果未能解决你的问题,请参考以下文章

Asp.Net Core 轻松学-经常使用异步的你,可能需要看看这个文章

C# ASP.NET Core Entity Framework Core 异步 ToQueryable 比较

C#和ASP.NET通过Gmail账户发送邮件的代码

C# asp.net mvc 判断代码是submit提交的还是异步提交的,如果是submit提交的,获取他是post 还是get方式

一个队asp.net session进行了再次封装的C#类的代码

C# ASP.NET MVC:提交按钮后不会存储数据