2012-12-14 86 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