【发布时间】:2018-04-06 22:35:30
【问题描述】:
我想让我的 TcpListen 线程使用委托更新 Form1 中的富文本框。 TcpListen 线程正在通过控制台工作和通信。但是,我无法让 Form1 中的富文本框附加文本。
public class MyTcpListener
{
public void Server()
{
// Some code that produces a string named data
SendText(data)
}
public delegate void TextDelegate(string message);
public void SendText(string message)
{
//Not sure how to send the string to Form1 without removing void and using return string?
}
public partial class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
MyTcpListener listen = new MyTcpListener();
tserv = new Thread(listen.Server);
//tserv.IsBackground = true;
tserv.Start();
//Where would I call the DisplayText method?
}
public void DisplayText(string message)
{
if (richTextBox1.InvokeRequired)
{
richTextBox1.Invoke(new MyTcpListener.CallBackDelegate(DisplayText),
new object[] { message });
}
【问题讨论】:
标签: c# multithreading delegates richtextbox