【发布时间】:2012-06-11 02:43:03
【问题描述】:
可能重复:
Updating UI from a different thread
The calling thread cannot access this object because a different thread owns it
祝大家有美好的一天。 我的聊天应用程序有问题,我需要一直刷新我的聊天但错误提示。我不知道如何解决这个问题。我希望有一个人可以帮助我。 这是我的代码:
void timerChatRefresh_Tick(object sender, EventArgs e)
{
thread = new Thread(new ThreadStart(ChatRefresh));
thread.Start();
}
private void ChatRefresh()
{
conn = new MySqlConnection("Server=...; Database=...; Uid=...; Password=...;");
ds.Clear();
da.SelectCommand = conn.CreateCommand();
da.SelectCommand.CommandText = "select * from chatmessagetbl";
da.SelectCommand.CommandType = CommandType.Text;
da.Fill(ds, "chatmessagetbl");
foreach (DataRow item in ds.Tables["chatmessagetbl"].Rows)
{
textBlockChatArea.Text += item["username"].ToString() + ": " + item["message"].ToString() + "\n";
}
}
【问题讨论】:
-
这段代码有很多问题:每个计时器滴答都有新线程;不处理连接;更新线程上的 UI 控件
-
对不起,我仍然是使用 Thread 类的初学者。我正在使用 wpf
标签: c# wpf multithreading