2

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.

Haseeb Asif
  • 1,766
  • 2
  • 23
  • 41
  • But that is not the nature of ASP.NET page life cycle. The code behind processes and when it is done the HTML is posted. With HTML5 I think you can update individual elements but I don't know how. – paparazzo Jul 28 '12 at 20:52
  • isn't there any way that i can use ajax or page methods to get partial update for each control on the page – Haseeb Asif Jul 28 '12 at 20:54

1 Answers1

0

The root problem is to understand the ASP.Net page life-cycle

Basically you are using multi-threading on your page, but the server will send the response to the user until the page has been completed processed.

To get your desired output, follow the following approach:

https://stackoverflow.com/a/11525692/1268570

For more info:

Community
  • 1
  • 1
Jupaol
  • 21,107
  • 8
  • 68
  • 100