C# Foreach冻结表
我知道每种冷冻形式都有很多东西,但我找不到解决问题的办法。我已经让这个程序的服务器部分工作了,我正在尝试使客户端连接到服务器时,这个代码将被执行C# Foreach冻结表,c#,foreach,scripting,C#,Foreach,Scripting,我知道每种冷冻形式都有很多东西,但我找不到解决问题的办法。我已经让这个程序的服务器部分工作了,我正在尝试使客户端连接到服务器时,这个代码将被执行txtConn.AppendText(“尝试连接”) 这是我的套接字连接代码 private static Socket ConnectSocket(string server, int port, RichTextBox txtConn, BackgroundWorker backgroundWorker1) { Socket
txtConn.AppendText(“尝试连接”)代码>
这是我的套接字连接代码
private static Socket ConnectSocket(string server, int port, RichTextBox txtConn, BackgroundWorker backgroundWorker1)
{
Socket s = null;
IPHostEntry hostEntry = null;
// Get host related information.
hostEntry = Dns.GetHostEntry(server);
// Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
// an exception that occurs when the host IP Address is not compatible with the address family
// (typical in the IPv6 case).
backgroundWorker1.RunWorkerAsync();
foreach (IPAddress address in hostEntry.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, port);
Socket tempSocket =
new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
Console.WriteLine(ipe);
try
{
attempt++;
txtConn.Select(txtConn.TextLength, 0);
txtConn.SelectionColor = Color.Aqua;
if (attempt == 1)
{
txtConn.AppendText("Attempting connection.");
}
else if (attempt > 1)
{
txtConn.AppendText("\r" + "Attempting connection.");
}
txtConn.SelectionColor = txtConn.ForeColor;
tempSocket.Connect(ipe);
}
catch (ArgumentNullException ane)
{
Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
txtConn.Select(txtConn.TextLength, 0);
txtConn.SelectionColor = Color.Red;
txtConn.AppendText("\r\n" + "Connection could not be established.");
txtConn.SelectionColor = txtConn.ForeColor;
}
catch (SocketException se)
{
Console.WriteLine("SocketException : {0}", se.ToString());
txtConn.Select(txtConn.TextLength, 0);
txtConn.SelectionColor = Color.Red;
txtConn.AppendText("\r\n" + "Connection could not be established.");
txtConn.SelectionColor = txtConn.ForeColor;
}
catch (Exception e)
{
Console.WriteLine("Unexpected exception : {0}", e.ToString());
txtConn.Select(txtConn.TextLength, 0);
txtConn.SelectionColor = Color.Red;
txtConn.AppendText("\r\n" + "Connection could not be established.");
txtConn.SelectionColor = txtConn.ForeColor;
}
if (tempSocket.Connected)
{
Console.WriteLine("Connected");
s = tempSocket;
break;
}
else
{
continue;
}
}
return s;
}
当我运行程序并连接到错误的端口时,它会检查我计算机上所有可能的IP,并等待foreach语句后显示错误或任何内容。如何使其主动显示此内容?您需要在不同的线程中运行代码,以便UI在执行时仍能更新
最简单的方法是将连接循环添加到线程池中的新任务中
ThreadPool.QueueUserWorkItem(i => {
// Connection loop goes here.
});
如果需要,还可以使用任务、BackgroundWorker等。使用应该使用Socket
类中的Async
方法,或者在另一个线程中运行这些东西。你也可以使用BackgroundWorker来做这件事。我刚才回答了一个类似的问题,但为了针对你的具体情况展开讨论,看起来你实际上并没有使用backgroundWorker1。foreach应该在backgroundWorker1.DoWork事件引用的方法中完成。您还需要为backgroundWorker1.ProgressChanged事件创建一个方法。您可以使用ReportProgress传递字符串,然后将该消息附加到文本框:
从Worker_DoWork方法中的foreach循环中,您将报告进度,而不是直接更新RichTextBox:
worker.ReportProgress(0, "Connection could not be established.");
然后在Worker_ProgressChanged方法中,您将使用类似以下内容来更新RichTextBox:
txtConn.AppendText(e.UserState.ToString());
每种冷冻形式?你是说这个程序没有反应吗?听起来你需要仔细阅读Thread
s。我知道你已经有了一个后台工作人员。它执行什么代码?为什么不将for循环放在后台work DoWork事件处理程序中?如何将字符串附加到richtextbox?在foreach循环中,您将使用消息调用worker.ReportProgress,而不是直接更新richtextbox。因此,在Worker_ProgressChanged方法中,您将执行类似于txtConn.AppendText(e.UserState.ToString())的操作。我会更新答案以反映这一点。谢谢你,这比其他任何事情都有帮助。谢谢你,蒂姆。