C# GUI中的线程
我有一个做端口扫描的工具,我遇到的问题是,当无法访问端点时,GUI会冻结,直到出现某种错误。我曾尝试创建一个线程,但我不太熟悉如何做。有人能告诉我怎么做吗C# GUI中的线程,c#,winforms,multithreading,C#,Winforms,Multithreading,我有一个做端口扫描的工具,我遇到的问题是,当无法访问端点时,GUI会冻结,直到出现某种错误。我曾尝试创建一个线程,但我不太熟悉如何做。有人能告诉我怎么做吗 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Win
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace PortScan
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timeTextBox.Text = "2000";
}
private void button1_Click(object sender, EventArgs e)
{
ThreadStart threadStart = GetPortStatus;
threadStart.BeginInvoke(null, null);
GetPortStatus();
}
private void GetPortStatus()
{
button1.Enabled = false;
var currentIP = ipaddressTextBox.Text;
int anInteger;
anInteger = Convert.ToInt32(portTextBox.Text);
anInteger = int.Parse(portTextBox.Text);
IPAddress IP = IPAddress.Parse(currentIP);
IPEndPoint EndPoint = new IPEndPoint(IP, anInteger);
Socket query = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//Console.WriteLine("Blocking: {0}", query.Blocking);
//resultsTextBox.Text = currentIP + ":" + anInteger + " is blocked: " + query.Blocking;
//resultsTextBox.Text += Environment.NewLine + currentIP + ":" + anInteger + " is blocked: " + query.Blocking;
try
{
query.Connect(EndPoint);
resultsTextBox.Text += "Connected to " + EndPoint + Environment.NewLine;
}
catch (SocketException i)
{
//Console.WriteLine("Problem connecting to host");
//Console.WriteLine(e.ToString());
resultsTextBox.Text += "Cannot connect to " + EndPoint + ", port maybe blocked" + Environment.NewLine;
query.Close();
button1.Enabled = true;
return;
}
//if (InvokeRequired)
//{
// Invoke(new MethodInvoker(Close));
//}
//else
//{
// Close();
//}
query.Close();
button1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (autoCheckBox.Checked == true)
{
button1_Click(sender, e);
}
else
{
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
应该是:
private void button1_Click(object sender, EventArgs e)
{
ThreadStart threadStart = new ThreadStart(GetPortStatus);
Thread name = new Thread(threadStart);
name.Start();
}
在threadstart构造函数中传递要调用的函数的委托。Threadstart表示在线程上执行的方法
应该是:
private void button1_Click(object sender, EventArgs e)
{
ThreadStart threadStart = new ThreadStart(GetPortStatus);
Thread name = new Thread(threadStart);
name.Start();
}
在threadstart构造函数中传递要调用的函数的委托。Threadstart表示在线程上执行的方法。第一个问题是,这是一个阻塞方法。在连接成功/被拒绝之前,它不会返回。您可以改为使用异步方法和。但这不是真正的问题,因为您已经在使用一个新线程 第二个问题是,不能从主线程以外的其他线程更新GUI
Invoke(new MethodInvoker(() =>
{
... // GUI updates (like your resultsTextBox)
}));
如果您不喜欢Invoke
这类东西,可以使用及其ReportProgress
方法+ProgressChanged
事件
@Hmm是正确的,因为您不使用新的线程
对象,所以在GUI线程上调用该方法。但是您需要调用更新,正如我上面所描述的
编辑
顺便说一句,最干净的解决方案是使用AOP()来完成任务。第一个问题是阻塞方法。在连接成功/被拒绝之前,它不会返回。您可以改为使用异步方法和。但这不是真正的问题,因为您已经在使用一个新线程 第二个问题是,不能从主线程以外的其他线程更新GUI
Invoke(new MethodInvoker(() =>
{
... // GUI updates (like your resultsTextBox)
}));
如果您不喜欢Invoke
这类东西,可以使用及其ReportProgress
方法+ProgressChanged
事件
@Hmm是正确的,因为您不使用新的线程
对象,所以在GUI线程上调用该方法。但是您需要调用更新,正如我上面所描述的
编辑
顺便说一句,最干净的解决方案是使用AOP()来完成任务。如果您构建的Windows窗体应用程序需要在单独的线程上执行任务,我建议您使用
BackgroundWorker
组件。关于如何使用此组件,有一个非常好的教程。如果您正在构建需要在单独线程上执行任务的Windows窗体应用程序,我建议您使用BackgroundWorker
组件。关于如何使用这个组件,有一个很好的教程。代码项目上提供了很好的资源:我认为最好的方法是使用。看一看这个!关于代码项目的好资源:我认为最好的方法是使用。看一看这个!