我目前正在嘗試制作即時消息傳遞應用程序。有一個客戶端和一個服務器。服務器工作正常,但是由于某種原因,當我調用某個函數來更新UI時,TextBox不會將文本添加到其中。下面是我的代碼示例-在我的應用程序中,以不同的形式調用Update UI:public ChatWindow() { InitializeComponent(); Thread timerThread = new Thread(Main.ReceiveLoop); timerThread.Start(); } private void txtChatLog_TextChanged(object sender, EventArgs e) { } private void btnSendMessage_Click(object sender, EventArgs e) { string clientReply = txtReply.Text; string Message = "ClientMsg§" + clientReply; var time = DateTime.Now; txtChatLog.AppendText($"{time} client: {clientReply}"); txtChatLog.AppendText(Environment.NewLine); Main main = new Main(); main.ChatResponse(Message); txtReply.Text = ""; } public void UpdateChatLog(string message) { var time = DateTime.Now; string newMessage = message.Split('$')[1]; string messageToDisplay = $"{time} Server: {newMessage}"; MessageBox.Show(messageToDisplay); txtChatLog.AppendText(messageToDisplay); txtChatLog.AppendText(Environment.NewLine); } private void ChatWindow_Load(object sender, EventArgs e) { }正如我用messagebox.show();檢查的那樣,客戶端挑釁地從服務器接收消息。同樣,當按下發送消息按鈕時,富文本框也會更新。但是由于某種原因,它不會通過UpdateChatLog方法進行更新。任何幫助將非常感激。
添加回答
舉報
0/150
提交
取消