您好,我有一个聊天应用程序,但不明白我哪里出错了,我所做的只是每秒做一个计时器,它会更新消息,m iageva static int wwitch = 1 或 = 0 来更新消息.. 当他们发送一个新消息将其更新为 1 代码很奇怪,有时我两次收到所有消息,或者有时他们不发送我只是想知道是否有任何其他方式可以做到这一点,例如教程或建议我的代码:
我在按钮前插入消息的代码!!
using (SqlDatabaseClient client = SqlDatabaseManager.GetClient())
{
int id = int.Parse(client.ExecuteScalar("SELECT COUNT(*) FROM program_messages").ToString());
client.SetParameter("id", id + 1);
client.SetParameter("sender", mUsername);
client.SetParameter("message", textBox1.Text);
client.SetParameter("timesent", DateTime.Now);
client.ExecuteNonQuery("INSERT INTO program_messages (`id`,`sender`,`message`,`timesent`) VALUES (@id,@sender,@message,@timesent)");
}
updateneeded = 1;
我的刷新消息代码
using (SqlDatabaseClient client6 = SqlDatabaseManager.GetClient())
{
int id = int.Parse(client6.ExecuteScalar("SELECT COUNT(*) FROM program_messages").ToString());
int finalcount = listBox2.Items.Count;
foreach (DataRow row2 in client6.ExecuteQueryTable("SELECT * FROM program_messages").Rows)
{
if (updateneeded > 0)
{
if (!listBox2.Items.Contains((string)row2["sender"] + ": " + (string)row2["message"]))
listBox2.Items.Add((string)row2["sender"] + ": " + (string)row2["message"]);
}
}
updateneeded = 0;
}