Torna al Thread
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading;
namespace RicercaHostSubnet
{
public partial class Form1 : Form
{
private List<String> lstHost = new List<String>();
private Thread myThread = null;
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
private void Form1_Load(object sender, EventArgs e)
{
txtIP.Text = "192.168.1"; //Prefisso dell'indirizzo IP deve essere inserito manualmente o crearsi una funzione a parte
}
private void cmdStart_Click(object sender, EventArgs e)
{
myThread = new Thread(() => scan(txtIP.Text));
myThread.Start();
if (myThread.IsAlive)
{
cmdStop.Enabled = true;
cmdScan.Enabled = false;
txtIP.Enabled = false;
}
}
private void cmdStop_Click(object sender, EventArgs e)
{
myThread.Suspend();
cmdScan.Enabled = true;
cmdStop.Enabled = false;
txtIP.Enabled = true;
}
public void scan(string subnet)
{
Ping myPing;
PingReply reply;
IPAddress addr;
IPHostEntry host;
for(int i = 1; i<255; i++)
{
string subnetn = "." + i.ToString();
myPing = new Ping();
reply = myPing.Send(subnet+subnetn);
if(reply.Status == IPStatus.Success)
{
try
{
addr = IPAddress.Parse(subnet + subnetn);
host = Dns.GetHostEntry(addr);
lstHost.Add(subnet + subnetn + " --> " + host.HostName.ToString()); //Entrambi salvati 192.168.0.200 --> HostName
lstIP.Items.Add(subnet+subnetn); //indirizzo IP
lstHostName.Items.Add(host.HostName.ToString()); //NomeHost
}
catch
{
}
}
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// Display a MsgBox asking the user to save changes or abort.
if (MessageBox.Show("Do you want close the program?", "Scan Subnetwork",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// Cancel the Closing event from closing the form.
e.Cancel = true;
}
}
//fine class
}
}