2012-12-14 84 views
-1

可能重複更新標籤文本:
How to update aspx page while using Multithreading如何使用多線程

我想,而在asp.net網站進行多線程更新標籤的文字,我的代碼工作正常,但它不更新標籤文本。當我調試它,然後它的工作正常,並更新代碼後面的標籤值,但它不會影響字體屏幕(頁面)。我的代碼是:

.ASPX代碼:

<form id="form1" runat="server"> 
    <div style="width:100%; text-align:center;"> 
     <asp:ScriptManager ID="ScriptManager1" runat="server"> 
     </asp:ScriptManager> 
     <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
      <ContentTemplate> 
     <br /> <br /> <br /> <br /> <br /> 
     Thread 1: <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <br /><br /> 
     Thread 2: <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> <br /><br /> 
     Thread 3: <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label> <br /><br /> 
     Thread 4: <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label> <br /><br /> 
     Thread 5: <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label> <br /><br /> 
     Thread 6: <asp:Label ID="Label6" runat="server" Text="Label"></asp:Label> <br /><br /> 
     Thread 7: <asp:Label ID="Label7" runat="server" Text="Label"></asp:Label> <br /> 
      </ContentTemplate> 
     </asp:UpdatePanel> 
    </div> 
    </form> 

.CS代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Threading; 
using System.Runtime.Remoting.Messaging; 

public partial class Default_test : System.Web.UI.Page 
{ 
    public delegate string Delg_Method1(int a, int b); 
    public delegate string Delg_Method2(int a, int b); 
    public delegate string Delg_Method3(int a, int b); 
    public delegate string Delg_Method4(int a, int b); 
    public delegate string Delg_Method5(int a, int b); 
    public delegate string Delg_Method6(int a, int b); 
    public delegate string Delg_Method7(int a, int b); 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     Label1.Text = "Waiting ..."; 
     Label2.Text = "Waiting ..."; 
     Label3.Text = "Waiting ..."; 
     Label4.Text = "Waiting ..."; 
     Label5.Text = "Waiting ..."; 
     Label6.Text = "Waiting ..."; 
     Label7.Text = "Waiting ..."; 
     callingAsynchMultiThreads(); 

    } 

    #region Threads 

    public void callingAsynchMultiThreads() 
    { 

     AsyncCallback method1Callback = new AsyncCallback(Method1Complete); 
     Delg_Method1 dlg_call1 = new Delg_Method1(Load_Method1); 
     IAsyncResult iar1 = dlg_call1.BeginInvoke(1, 0, method1Callback, null); 

     AsyncCallback method2Callback = new AsyncCallback(Method2Complete); 
     Delg_Method2 dlg_call2 = new Delg_Method2(Load_Method2); 
     IAsyncResult iar2 = dlg_call2.BeginInvoke(1, 1, method2Callback, null); 

     AsyncCallback method3Callback = new AsyncCallback(Method3Complete); 
     Delg_Method3 dlg_call3 = new Delg_Method3(Load_Method3); 
     IAsyncResult iar3 = dlg_call3.BeginInvoke(1, 2, method3Callback, null); 

     AsyncCallback method4Callback = new AsyncCallback(Method4Complete); 
     Delg_Method4 dlg_call4 = new Delg_Method4(Load_Method4); 
     IAsyncResult iar4 = dlg_call4.BeginInvoke(1, 3, method4Callback, null); 

     AsyncCallback method5Callback = new AsyncCallback(Method5Complete); 
     Delg_Method5 dlg_call5 = new Delg_Method5(Load_Method5); 
     IAsyncResult iar5 = dlg_call5.BeginInvoke(1, 4, method5Callback, null); 

     AsyncCallback method6Callback = new AsyncCallback(Method6Complete); 
     Delg_Method6 dlg_call6 = new Delg_Method6(Load_Method6); 
     IAsyncResult iar6 = dlg_call6.BeginInvoke(1, 5, method6Callback, null); 

     AsyncCallback method7Callback = new AsyncCallback(Method7Complete); 
     Delg_Method7 dlg_call7 = new Delg_Method7(Load_Method7); 
     IAsyncResult iar7 = dlg_call7.BeginInvoke(1, 6, method7Callback, null); 


    } 
    public string Load_Method1(int a, int b) 
    { 
     int temp = a + b; 
     return temp.ToString(); 
    } 
    public string Load_Method2(int a, int b) 
    { 
     int temp = a + b; 
     return temp.ToString(); 
    } 
    public string Load_Method3(int a, int b) 
    { 
     int temp = a + b; 
     return temp.ToString(); 
    } 
    public string Load_Method4(int a, int b) 
    { 
     int temp = a + b; 
     return temp.ToString(); 
    } 
    public string Load_Method5(int a, int b) 
    { 
     int temp = a + b; 
     return temp.ToString(); 
    } 
    public string Load_Method6(int a, int b) 
    { 
     int temp = a + b; 
     return temp.ToString(); 
    } 
    public string Load_Method7(int a, int b) 
    { 
     int temp = a + b; 
     return temp.ToString(); 
    } 

    public void Method1Complete(IAsyncResult ar) 
    { 
     Thread.Sleep(500); 
     Delg_Method1 dlgM1 = (Delg_Method1)((AsyncResult)ar).AsyncDelegate; 
     //dlgM1.EndInvoke(ar); 
     Label1.Text = dlgM1.EndInvoke(ar).ToString(); 


    } 
    public void Method2Complete(IAsyncResult ar) 
    { 
     Thread.Sleep(1000); 
     Delg_Method2 dlgM2 = (Delg_Method2)((AsyncResult)ar).AsyncDelegate; 
     //dlgM2.EndInvoke(ar); 
     Label2.Text = dlgM2.EndInvoke(ar).ToString(); 
    } 
    public void Method3Complete(IAsyncResult ar) 
    { 
     Thread.Sleep(1500); 
     Delg_Method3 dlgM3 = (Delg_Method3)((AsyncResult)ar).AsyncDelegate; 
     //dlgM3.EndInvoke(ar); 
     Label3.Text = dlgM3.EndInvoke(ar).ToString(); 
    } 
    public void Method4Complete(IAsyncResult ar) 
    { 
     Thread.Sleep(2000); 
     Delg_Method4 dlgM4 = (Delg_Method4)((AsyncResult)ar).AsyncDelegate; 
     //dlgM4.EndInvoke(ar); 
     Label4.Text = dlgM4.EndInvoke(ar).ToString(); 
    } 
    public void Method5Complete(IAsyncResult ar) 
    { 
     Thread.Sleep(2500); 
     Delg_Method5 dlgM5 = (Delg_Method5)((AsyncResult)ar).AsyncDelegate; 
     //dlgM5.EndInvoke(ar); 
     Label5.Text = dlgM5.EndInvoke(ar).ToString(); 
    } 
    public void Method6Complete(IAsyncResult ar) 
    { 
     Thread.Sleep(3000); 
     Delg_Method6 dlgM6 = (Delg_Method6)((AsyncResult)ar).AsyncDelegate; 
     //dlgM6.EndInvoke(ar); 
     Label6.Text = dlgM6.EndInvoke(ar).ToString(); 
    } 
    public void Method7Complete(IAsyncResult ar) 
    { 
     Thread.Sleep(3500); 
     Delg_Method7 dlgM7 = (Delg_Method7)((AsyncResult)ar).AsyncDelegate; 
     //dlgM7.EndInvoke(ar); 
     Label7.Text = dlgM7.EndInvoke(ar).ToString(); 

    } 


    #endregion 

} 

回答

1

這是因爲頁面生命週期已結束,頁面渲染/發在線程完成更新其控件之前到瀏覽器。在調試期間,您可以看到完成工作的線程,但正在修改已發送到瀏覽器的標籤。

一旦頁面已經被髮送到瀏覽器服務器無法再發送一個新的版本,客戶端無需客戶端要求一個「新版本」。一個可能的解決方法是讓頁面不斷地通過AJAX/JavaScript或類似的方式檢查更新,當線程完成時可以顯示新的值。

更新頁面的VERY簡單示例每5秒使用UpdatePanel將是:

Web頁(CSS)

<asp:UpdatePanel ID="updateView" runat="server" > 
<ContentTemplate> 

    <!-- Refresh the page automatically --> 
    <asp:Timer ID="timAutoRefresh" runat="server" Interval="5000" OnTick="OnRefresh_Tick" /> 

</ContentTemplate> 
</asp:UpdatePanel> 

代碼隱藏(的.cs)

protected void OnRefresh_Tick(object sender, EventArgs e) 
{  
    // Check values and update page... 
} 

有超視距呃方式,這是一個非常簡單的例子。您應該添加標籤到相同的UpdatePanel作爲定時器和改變的UpdatePanel所以它僅更新自身,以儘量減少後背上等

+0

呀,你是對的,我也知道這個生命週期,但問題是,如何能我們這樣做?你能幫我解答嗎? 感謝 –

+0

希望這將讓你開始!如果您喜歡答案,請點擊TICK並將其標記爲答案。謝謝。 – Belogix

+1

他跟你說過了:「對此的一種可能的方式是在頁面不斷地檢查通過AJAX/JavaScript或類似的更新,所以當線程完成可以顯示新的價值。」 – tomfanning