У меня есть инструмент, который выполняет сканирование портов, проблема, с которой я сталкиваюсь, заключается в том, что когда конечная точка недоступна, графический интерфейс зависает, пока не получится какая-то ошибка. Я попытался создать поток, но я не слишком хорошо разбираюсь в том, как это сделать. Может ли кто-нибудь показать мне, как?Threading in GUI
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)
{
}
}
}
Хороший доступный ресурс по проекту кода: http://www.codeproject.com/KB/threads/CSharpAsynchronousHelper2.aspx – Hmm
Я думаю, что лучший способ - использовать [PostSharp] (http://www.sharpcrafters.com/)). Взгляните на [OnGuiThreadAttribute] (http://www.sharpcrafters.com/solutions/multithreading)! – Matthias
Спасибо, обязательно прочитайте, выглядит интересно. – Hmm