利用多线程asp.net更新UI [英] asp.net update UI using multi-thread

查看:1168
本文介绍了利用多线程asp.net更新UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一个ASP.NET网站的以下

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 have created a thread function. And during the execution of this function I would like to update some controls on 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);
    }
}

我怎么会更新一个线程执行期间Web的控制?我需要给力的UpdatePanel1到后回?怎么样?

How could I update a web-control during a thread-execution? do I need to force the "UpdatePanel1" to post-back? how?

推荐答案

您需要使用一个客户端计时器(或其他方式)来让浏览器请求更新,服务器像这样的简单示例

You'll need to use a client-side timer (or some other method) to have the browser ask the server for the update, such as 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>

然后在你的codebehind:

Then in your codebehind:

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

如果您有更新某些状态一个后台进程,你要么需要共享状态存储在会话,HTTP缓存或数据库。需要注意的是高速缓存可以终止由于多种因素,如果IIS回收应用程序池后台线程可以杀死任何的领带。

If you have a background process that is updating some state, you will either need to store the shared state in the session, http cache, or a database. Note that the cache can expire due to many factors, and background threads can be killed any any tie if IIS recycles the application pool.

这篇关于利用多线程asp.net更新UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆