I am working on a page which will show current state of the system by getting data from different servers.
On my page i have five different labels and a button. I want once i click the button five different threads get data from servers and update the corresponding label value. I have made a test application with the following code
protected void Page_Load(object sender, EventArgs e)
{
Thread t = new Thread(updateLbl);
t.Start();
}
protected void btnUpdateStatus_Click(object sender, EventArgs e)
{
Thread[] arrThread = new Thread[5];
for (int i = 0; i < arrThread.Length; i++)
{
arrThread[i] = new Thread(new ParameterizedThreadStart(Update));
arrThread[i].Name = "Thread" + (i + 1).ToString();
arrThread[i].Start(
new Parameters()
{
id = i + 1,
label = this.Master.FindControl("MainContent").FindControl ("Label" + (i + 1).ToString()) as Label
} as object);
}
}
public void Update(object o)
{
Parameters p = o as Parameters;
if (p != null && p.label != null)
{
p.label.Text = GetSystemState(p.id);
}
}
class Parameters
{
public int id { get; set; }
public Label label { get; set; }
}
It does update the values of the labels but page keep on loading until all the values from servers are retrieved and then display them at once on the page.
I want some thing that if a thread has retrieved it;s data, it's corresponding label must be updated and available to customer whether other process have retrieved their data or not and they must be getting updated as soon the data is retrieved.