我有一個在Web應用程序中花費大量時間的功能。 所以我決定爲它創建一個線程。頁面加載後刷新標籤
Thread t = new Thread(getEventErrors);
t.Start();
該函數計算一個值並將其應用於標籤中。
這是一個問題,因爲在asp.net頁面加載一次,並沒有更新標籤到那時。
如何實現這個使用AJAX?
是否有在頁面加載和計算值後的標籤被更新的方法嗎?
任何樣品代碼將高度讚賞。
感謝
private void getEventErrors()
{
EventLog eventLog = new EventLog("Application", ".");
getEvents(eventLog.Entries);
}
private void getEvents(EventLogEntryCollection eventLogEntryCollection)
{
int errorEvents = 0;
foreach (EventLogEntry logEntry in eventLogEntryCollection)
{
if (logEntry.Source.Equals("Application Name"))
{
DateTime variable = Convert.ToDateTime(logEntry.TimeWritten);
long eventTimeTicks = (variable.Ticks);
long eventTimeUTC = (eventTimeTicks - 621355968000000000)/10000000;
long presentDayTicks = DateTime.Now.Ticks;
long daysBackSeconds = ((presentDayTicks - 864000000000) - 621355968000000000)/10000000;
if (eventTimeUTC > daysBackSeconds)
{
if (logEntry.EntryType.ToString() == "Error")
{
errorEvents = errorEvents + 1;
}
}
}
}
btn_Link_Event_Errors_Val.Text = errorEvents.ToString(GUIUtility.TWO_DECIMAL_PT_FORMAT);
if (errorEvents == 0)
{
lbl_EventErrorColor.Attributes.Clear();
lbl_EventErrorColor.Attributes.Add("class", "green");
}
else
{
lbl_EventErrorColor.Attributes.Clear();
lbl_EventErrorColor.Attributes.Add("class", "red");
}
}
這是代碼..但我想處理所有的長時間使用AJAX耗時代碼。
例如,頁面應該加載速度很快,並且long函數應該在等待遊標的線程中繼續加載,當該值被獲取時,它會顯示在標籤或任何其他控件中。
需要多長時間? 5秒鐘或5分鐘? – Aristos 2012-04-02 18:30:22
你應該開始優化數據庫查詢/頁面事件等等。它需要很多內存。 – Pankaj 2012-04-02 18:34:18
@Aristos:加載第一頁需要7-8秒,如果我刪除該功能需要3 - 4秒。 – user175084 2012-04-02 18:44:38