问题描述
我在asp.net中有一个项目。我试图加载10,000条记录的数据,服务器在达到一段时间后停止响应。因此,我尝试调用异步方法,并在加载数据后向用户
发送电子邮件。一切正常,但我在我的页面中使用会话变量 。有人可以告诉我如何在异步方法中使用调用会话变量。
这是我的使用异步方法的代码:
protected void btnAdd_Click(object sender,EventArgs e){
$
          lblErrorMessageAtTheButtom.Text ="已完成"; $
          AsyncMethodCaller caller = new AsyncMethodCaller(LongTimeTask);
          IAsyncResult result = caller.BeginInvoke(" TestString",new AsyncCallback(TaskCompleted),caller);
      }
public void LongTimeTask(string s){
          // 这里耗时的任务
  // To To方法
    }
public void TaskCompleted(IAsyncResult ar){
          AsyncMethodCaller d =(AsyncMethodCaller)ar.AsyncState;
          //结束加载方法调用
          d.EndInvoke(ar);
      }
这是我的会话变量i 在To do方法中使用:
public static string UserId {
         得到{
              if(HttpContext.Current.Session [" UserId"] == null)
                  return string.Empty;
             否则
                  return(string)HttpContext.Current.Session [" UserId"];
          }¥b $ b          设置{
              HttpContext.Current.Session [" UserId"] = value;
          }¥b $ b       }
可以有人请来我救援&引导我?我完全迷失了我想知道
我是否朝着正确的方向前进!
将提供更多信息。
I have a project in asp.net. I am trying to load the data for 10,000 records and the server stops responding after it reaches some time. So im trying to call the async method and send an email to user once the data is loaded. Everything works fine but i use session variables in my page. Can some one tell me how to use call session variables in async method.
Here is my code using async method:
protected void btnAdd_Click(object sender, EventArgs e) {
lblErrorMessageAtTheButtom.Text = "Completed";
AsyncMethodCaller caller = new AsyncMethodCaller(LongTimeTask);
IAsyncResult result = caller.BeginInvoke("TestString", new AsyncCallback(TaskCompleted), caller);
}
public void LongTimeTask(string s) {
// time consuming task here
//To Do metthod
}
AsyncMethodCaller d = (AsyncMethodCaller)ar.AsyncState;
//end the Load method call
d.EndInvoke(ar);
}
Here are my session variables i use in To do method:
public static string UserId {get {
if (HttpContext.Current.Session["UserId"] == null)
return string.Empty;
else
return (string)HttpContext.Current.Session["UserId"];
}
set {
HttpContext.Current.Session["UserId"] = value;
}
}
Could someone please come to my rescue & guide me? I am totally lost & would like to know if I am heading in the right direction!
More information will be provided.
这篇关于如何在asp.net中使用c#在Asynchronous方法中使用会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!