【发布时间】:2015-06-26 17:11:18
【问题描述】:
当我尝试从另一个类访问表单控件时遇到问题。我的程序挂在无限循环中。我知道为什么,但我不知道如何正确地写这个。
这是 Form1.cs(到我的表单)
public Form1()
{
InitializeComponent();
Load config = new Load();
string[] data = config.readConfig("config.ini");
if (data.Length == 4) { //client
Client run = new Client();
run.startClient(data[1], Convert.ToInt32(data[2]));
}
else if (data.Length == 3) //server
{
Server run = new Server();
run.startServer(Convert.ToInt32(data[1]));
}
}
public void addLog(string dataLog){
richTextBox1.Text += dataLog;
}
这是 Client.cs 文件:
class Client
{
public void startClient(string ipAddr, int port)
{
Form1 form1 = new Form1();
TcpClient client = new TcpClient();
try
{
form1.addLog("Connecting...");
client.Connect(ipAddr, port);
form1.addLog("Connected to server: " + ipAddr + ":" + port.ToString());
}
catch
{
MessageBox.Show("We couldn't connect to server");
}
}
}
如何在不运行每次新表单的情况下更改文本值。也许有类似 run_once 的东西?
【问题讨论】:
标签: c# winforms infinite-loop