可能重复:
如何在使用多线程时更新 aspx 页面
我想在 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
}