我有一個簡單的頁面和事件列表。點擊按鈕後,需要轉到「與會者」頁面並在該事件中加載人員。page_load上的相當簡單的asp.net異步操作阻塞UI
現在發生的情況是,單擊按鈕後,事件頁面將保持可見狀態,直到所有與會者處理完成並完成,然後顯示結果。我希望它首先顯示來自第一個異步調用的事件名稱,然後我可以在頁面上顯示進度條,以顯示正在構建的與會者列表。如果直接使用查詢字符串中的事件ID加載「與會者」頁面,它將根據需要運行,並顯示事件名稱和進度條。
我不知道如何設置(異步noob)來獲得所需的結果,並會欣賞一些指針請!
在events.aspx.cs按鈕單擊處理程序:
Response.Redirect("Attendees.aspx?eventID=" + eventID, false);
然後在Attendees.aspx.cs的Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
processEvent();
//I've tried this with .Wait() at the end too
}
private async Task<bool> processEvent()
{
try
{
EFEventDetails efEvent = await getEventByIdAsync();
if (efEvent.responseCode != 200) //only 200 is a valid response code for a found event
{
pnl_loadingError.Visible = true;
pnl_attendees.Visible = false;
}
else
{
//valid event - carry on. Display event name on page.
lbl_EventHeader.Text = efEvent.data.eventID.ToString() + " - " + efEvent.data.eventName;
//Now get list of attendees
List<vmAttendees> res = await getAttendeesAsync();
if (res.Count > 0)
{
pnl_AttendeeList.Visible = true;
rpt_AttendeeList.DataSource = res;
rpt_AttendeeList.DataBind();
}
}
}
catch (Exception ex)
{
lbl_Result.Text = ex.Message;
lbl_Result.ForeColor = System.Drawing.Color.Red;
pnl_processResult.Visible = true;
}
return true;
}
protected async Task<EFEventDetails> getEventByIdAsync()
{
var eventsForceService = new EventsForceService(_eventsForceRepo);
return await eventsForceService.getEventByIdAsync(_eventID);
}
protected async Task<List<vmAttendees>> getAttendeesAsync()
{
var eventsForceService = new EventsForceService(_eventsForceRepo);
return await eventsForceService.GetAttendeesByEventIDAsync(_eventID);
}
這不會解決你的如何獲得工作的進度條問題,但你應該閱讀[文章](HTTPS ://www.hanselman.com/blog/TheMagicOfUsingAsynchronousMethodsInASPNET45PlusAnImportantGotcha.aspx)在'Page_Load'中看到正確的事情(你需要調用RegisterAsyncTask(processEvent());確保你的aspx頁面有'Async =如果你只是想等到頁面爲fu,那麼它就是「Page」元素中的「true」在展示任何東西之前裝載。 –
謝謝斯科特。如果你檢查我的原始帖子,你可以看到我已經有進度欄的東西工作得很好,如果我只是直接加載頁面。我正在使用SignalR和事件處理程序。我正在閱讀您現在發佈的文章。謝謝。 – TheMook