Page_Load()是否可以异步 [英] Can Page_Load() Be Async
问题描述
Page_Load()
方法可以是async
吗?我问我是否已声明如此
protected void Page_Load()
一切都应加载.如果我这样声明
protected async void Page_Load()
Page_Load()
断点未命中,catch()
块也未命中.
现在,我试图将我的Page_Load()
方法设置为async
,以便在完全呈现页面之前执行3个不同的存储过程以完成操作.如果我没有Page_Load()
方法作为async
,则会收到此编译错误:
await运算符只能与异步方法一起使用.
我的代码就是这样.
private DataSet ds1 = new DataSet();
private DataSet ds2 = new DataSet();
private DataSet ds3 = new DataSet();
protected async void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var task1 = GetStoreInfo();
var task2 = GetSalespersonInfo();
var task3 = GetManagerInfo();
await System.Threading.Tasks.Task.WhenAll(task1, task2, task3);
PopulateAll();
}
}
async System.Threading.Tasks.Task<DataSet> GetStoreInfo()
{
ds1 = RunStoredProcedureToReturnThisData();
return ds1;
}
async System.Threading.Tasks.Task<DataSet> GetSalespersonInfo()
{
ds2 = RunStoredProcedureToReturnThisData();
return ds2;
}
async System.Threading.Tasks.Task<DataSet> GetManagerInfo()
{
ds3 = RunStoredProcedureToReturnThisData();
return ds3;
}
protected void PopulateAll()
{
//Bind the different returned datasets
}
Scott Hanselman可以将异步与ASP.NET生命周期事件一起使用
> http://www.hanselman.com/blog/TheMagicOfUsingAsynchronousMethodsInASPNET45PlusAnasp.x/p> Can a Everything loads as it should. If I have it declared as such the Now I am trying to set my The await operator can only be used with an async method. My code is as such. }
Scott Hanselman has the magic to use async with ASP.NET lifecycle events here http://www.hanselman.com/blog/TheMagicOfUsingAsynchronousMethodsInASPNET45PlusAnImportantGotcha.aspx 这篇关于Page_Load()是否可以异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!Page_Load()
method be async
? I ask as if I have declared as suchprotected void Page_Load()
protected async void Page_Load()
Page_Load()
breakpoint is not hit, nor does the catch()
block get hit.Page_Load()
method as async
in order to have 3 different stored procedures execute to completion before the page is fully rendered. If I do not have my Page_Load()
method as async
I get this compile error:
private DataSet ds1 = new DataSet();
private DataSet ds2 = new DataSet();
private DataSet ds3 = new DataSet();
protected async void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var task1 = GetStoreInfo();
var task2 = GetSalespersonInfo();
var task3 = GetManagerInfo();
await System.Threading.Tasks.Task.WhenAll(task1, task2, task3);
PopulateAll();
}
async System.Threading.Tasks.Task<DataSet> GetStoreInfo()
{
ds1 = RunStoredProcedureToReturnThisData();
return ds1;
}
async System.Threading.Tasks.Task<DataSet> GetSalespersonInfo()
{
ds2 = RunStoredProcedureToReturnThisData();
return ds2;
}
async System.Threading.Tasks.Task<DataSet> GetManagerInfo()
{
ds3 = RunStoredProcedureToReturnThisData();
return ds3;
}
protected void PopulateAll()
{
//Bind the different returned datasets
}