Asp.net update interface using multiple threads

I have an ASP.NET website containing the following

<asp:UpdatePanel ID="UpdatePanel1" runat="server" > <ContentTemplate> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </ContentTemplate> </asp:UpdatePanel> 

I created a stream function. And during the execution of this function, I would like to update some controls in the user interface.

 protected void Page_Load(object sender, EventArgs e) { new Thread(new ThreadStart(Serialize_Click)).Start(); } protected void Serialize_Click() { for (int i = 1; i < 10; i++) { Label1.Text = Convert.ToString(i); UpdatePanel1.Update(); System.Threading.Thread.Sleep(1000); } } 

How can I update a web control at runtime? Do I need to get "UpdatePanel1" to go back? as?

+6
multithreading user-interface
source share
3 answers

You will need to use the client timer (or some other method) so that the browser requests an update from the server, for example, this simplified example:

 <asp:UpdatePanel ID="up" runat="server"> <ContentTemplate> <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="timer_Ticked" /> <asp:Label ID="Label1" runat="server" Text="1" /> </ContentTemplate> </asp:UpdatePanel> 

Then in your code:

 protected void timer_Ticked(object sender, EventArgs e) { Label1.Text = (int.Parse(Label1.Text) + 1).ToString(); } 

If you have a background process that updates some state, you need to either save the general state in the session, or in the http cache, or in the database. Please note that the cache can expire due to many factors, and background threads can be killed by any link if IIS is processing the application pool.

+6
source share

It will not do what you think.

It would be wiser to use the front-panel js timer to invoke a web method that refreshes the page.

Remember that on the server, as soon as the page is displayed to you, it no longer exists until it is called again. The Internet is a stateless environment. Postback and viewstate make you feel that this is not so, but it really is.

Thus, you cannot call the client from the server, what are you trying to do.

+1
source share

You will need to use javascript / jquery to poll the server for changes. This is one of the most unfortunate side effects of all this fancy-schmancy jquery / ajax stuff: people forget that the browser needs to poll the web server - this is a one-way connection, even if it sometimes seems to be a client of the winforms app. Ultimately, the browser has to make an http message / receive the request yourself. Yes, you can run javascript client in advance to say this, but this requires you to know exactly when to call back. It looks like you have some kind of arbitrary code that will be executed when it is done, so in this case you should constantly poll.

-Oisin

0
source share

All Articles