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

时间:2015-05-07 08:41:16

标签: c# asp.net multithreading gridview

这个网络应用程序的基本构思是一个聊天室,显示聊天记录的GridView应该通过从数据库中选择最新的聊天记录并存储在DataTable中的DataSource来刷新。 GridviewGridView的标记如下: -

    <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 = "";
            }

        }

}

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

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

1 个答案:

答案 0 :(得分:1)

使用&#39;线程。线程&#39;用于独立执行函数和Threading.Timer以间隔调用该函数。

声明Threading.Timer:

$ cat assert.swift 
assert(false, "assertion asserted")
println("made it here...")
$ swiftc -Onone assert.swift; ./assert
assertion failed: assertion asserted: file assert.swift, line 1
Illegal instruction: 4
$ swiftc -O assert.swift; ./assert
made it here...
$ swiftc -O -assert-config Debug assert.swift; ./assert
assertion failed: assertion asserted: file assert.swift, line 1
Illegal instruction: 4
$ 

声明线程。线程:

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

定期运行的方法:

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

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

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