如何定期调用访问和更新 UI 控件的函数?

How to call a function at regular interval which accesses and updates UI controls?

此 Web 应用程序的基本理念是聊天室,显示聊天记录的 GridView 应通过从数据库中选择最新聊天记录并存储在 DataTable 中进行刷新,即 DataSourceGridviewGridView 的标记如下:-

    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
        Width="545px" HeaderStyle-BorderStyle="None" HeaderStyle-Height="0px">
        <Columns>

            <asp:TemplateField>
                <HeaderStyle Height="0px" Wrap="False" />
                <ItemStyle BackColor="#CCCCCC" ForeColor="Black" Width="394px" 
                    BorderStyle="None" />
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("user") %>'></asp:Label>
                    <asp:Label ID="Label4" runat="server" Text=": "></asp:Label>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("chat") %>'></asp:Label><br />
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("date") %>' Font-Size="Small" ForeColor="Gray"></asp:Label>
                 </ItemTemplate>
            </asp:TemplateField>


        </Columns>
        <HeaderStyle BorderStyle="None" Height="0px" />
    </asp:GridView>

当然还有另一个 GridView 包含用户名。单击其中一个时,将触发 RowCommand 事件:-

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{

    int rowValue = Convert.ToInt32(e.CommandArgument.ToString());
    GridView1.SelectedIndex = rowValue;
    show_chats();

}

最后,这里是 show_chats 函数:-

    public void show_chats()
{
        if (GridView1.SelectedIndex >= 0)
        {
            LinkButton ul = (LinkButton)GridView1.Rows[GridView1.SelectedIndex].FindControl("userList");
            string gridViewValue = ul.Text;
            conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter("select * from chats where (chatFrom='" + user + "' and chatTo='" + gridViewValue + "') OR (chatFrom='" + gridViewValue + "' and chatTo='" + user + "')  order by dateTime", conn);
            conn.Close();
            DataTable dt = new DataTable();
            sda.Fill(dt);
            if (dt.Rows.Count == 0)
            {
                txtChat.Text = "No past chats, Type here to get started";
                chats.Clear();
                GridView2.DataSource = chats;
                GridView2.DataBind();
            }
            else
            {
                chats.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow row = chats.NewRow();
                    row[0] = dt.Rows[i][0].ToString();
                    row[1] = dt.Rows[i][2].ToString();
                    row[2] = dt.Rows[i][3].ToString();
                    chats.Rows.Add(row);
                }
                GridView2.DataSource = chats;
                GridView2.DataBind();
                txtChat.Text = "";
            }

        }

}

现在我希望在从 GridView1(包含用户名)中选择用户时定期调用 show_chats 函数来检查新消息。

我尝试使用线程,但我想我做错了,因为它抛出的异常在我不使用线程时不会抛出。 如果我必须使用线程,请告诉我如何正确使用它。

使用 'Threading.Thread' 独立执行函数,Threading.Timer 间隔调用该函数。

正在声明 Threading.Timer :

System.Threading.Timer TimerForXYZ = new System.Threading.Timer(TickForXYZ, null, 0, 1000); // this will call method TickForXYZ after every 1 second

正在声明 Threading.Thread :

    private void TickForXYZ(object obj)
    {
        Thread myThreadXYZ = new Thread(new System.Threading.ThreadStart(XYZ));
        myThreadXYZ .IsBackground = true;
        myThreadXYZ .Start();
    }

您定期 运行 的方法:

private void XYZ()
{
    // Your code goes here
    // This function will run after every 1s
}

或者,您可以使用 javascript 中的计时器来调用 ajax 函数,这将加载您的网格。

如果您需要任何帮助,请告诉我。