|
using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Using System.Net;
Using System.Threading;
Namespace WindowLanSearch
{
///
/// Summary description of Form1.
///
Public class Form1: System.Windows.Forms.Form
{
Private System.Windows.Forms.TextBox textBox1;
Private System.Windows.Forms.Button button1;
Private string [,] LanHost;
Private System.Windows.Forms.ProgressBar progressBarSearch;
Private Thread [] thread;
Private System.Windows.Forms.ListView listView1;
Private System.Windows.Forms.ColumnHeader columnHeader1;
Private System.Windows.Forms.ColumnHeader columnHeader2;
Private string str;
///
/// Required designer variables.
///
Private System.ComponentModel.Container components = null;
Public Form1 ()
{
//
// Required for Windows Forms Designer support
//
InitializeComponent ();
InitLanHost ();
ProgressBarSearch.Maximum = 255;
//
TO // TODO: Add any constructor code after the InitializeComponent call
//
}
///
/// array initialization
///
Private void InitLanHost ()
{
LanHost = new string [255,2];
For (int i = 0; i <255; i ++)
{
LanHost [i, 0] = "";
LanHost [i, 1] = "";
}
}
///
/// Clean up all resources in use.
///
Protected override void Dispose (bool disposing)
{
If (disposing)
{
If (components! = Null)
{
Components.Dispose ();
}
}
Base.Dispose (disposing);
}
#Region Code generated by Windows Forms Designer
///
/// Method required for designer support-do not use code editor to modify
/// The contents of this method.
///
Private void InitializeComponent ()
{
This.textBox1 = new System.Windows.Forms.TextBox ();
This.button1 = new System.Windows.Forms.Button ();
This.progressBarSearch = new System.Windows.Forms.ProgressBar ();
This.listView1 = new System.Windows.Forms.ListView ();
This.columnHeader1 = new System.Windows.Forms.ColumnHeader ();
This.columnHeader2 = new System.Windows.Forms.ColumnHeader ();
This.SuspendLayout ();
//
// textBox1
//
This.textBox1.Location = new System.Drawing.Point (24, 40);
This.textBox1.Multiline = true;
This.textBox1.Name = "textBox1";
This.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
This.textBox1.Size = new System.Drawing.Size (176, 296);
This.textBox1.TabIndex = 0;
This.textBox1.Text = "";
//
// button1
//
This.button1.Location = new System.Drawing.Point (456, 40);
This.button1.Name = "button1";
This.button1.TabIndex = 1;
This.button1.Text = "Start search";
This.button1.Click + = new System.EventHandler (this.button1_Click);
//
// progressBarSearch
//
This.progressBarSearch.Location = new System.Drawing.Point (32, 360);
This.progressBarSearch.Name = "progressBarSearch";
This.progressBarSearch.Size = new System.Drawing.Size (490, 24);
This.progressBarSearch.TabIndex = 2;
//
// listView1
//
This.listView1.Columns.AddRange (new System.Windows.Forms.ColumnHeader [] {
This.columnHeader1,
This.columnHeader2});
This.listView1.Location = new System.Drawing.Point (248, 40);
This.listView1.Name = "listView1";
This.listView1.Size = new System.Drawing.Size (184, 288);
This.listView1.TabIndex = 5;
//
// columnHeader1
//
This.columnHeader1.Text = "dddd";
//
// columnHeader2
//
This.columnHeader2.Text = "sssss";
//
// Form1
//
This.AutoScaleBaseSize = new System.Drawing.Size (6, 14);
This.ClientSize = new System.Drawing.Size (544, 413);
This.Controls.Add (this.listView1);
This.Controls.Add (this.progressBarSearch);
This.Controls.Add (this.button1);
This.Controls.Add (this.textBox1);
This.Name = "Form1";
This.Text = "Form1";
This.ResumeLayout (false);
}
#Endregion
///
/// The main entry point for the application.
///
[STAThread]
Static void Main ()
{
Application.Run (new Form1 ());
}
Private void button1_Click (object sender, System.EventArgs e)
{
LanSearch ();
}
///
/// LAN search event
///
Private void LanSearch ()
{
Thread = new Thread [255];
ThreadStart threadMethod;
Thread threadProgress = new Thread (new ThreadStart (progressSearch));
ThreadProgress.Start ();
String localhost = (Dns.GetHostByName (Dns.GetHostName ())). AddressList [0] .ToString (); // Local host IP address
Str = localhost.Substring (0, localhost.LastIndexOf ("."));
For (int i = 0; i <255; i ++) // Create 255 threads to scan IP
{
ThreadMethod = new ThreadStart (LanSearchThreadMethod);
Thread [i] = new Thread (threadMethod);
Thread [i] .Name = i.ToString ();
Thread [i] .Start ();
If (! Thread [i] .Join (100)) //Thread.Join(100) I do n’t know if it is used this way, right?
{
Thread [i] .Abort ();
}
}
GetLanHost ();
ListLanHost ();
}
///
/// Multithreaded search method
///
Private void LanSearchThreadMethod ()
{
Int Currently_i = Convert.ToUInt16 (Thread.CurrentThread.Name); // Current process name
IPAddress ScanIP = IPAddress.Parse (str + "." + Convert.ToString (Currently_i +1)); // Get scan IP address
IPHostEntry ScanHost = null;
HostScanHost = Dns.GetHostByAddress (ScanIP); // Get host information of scan IP address
If (ScanHost! = Null)
{
LanHost [Currently_i, 0] = ScanIP.ToString ();
LanHost [Currently_i, 1] = ScanHost.HostName;
}
//ProgressBarSearch.Value = progressBarSearch.Value +1;
}
///
/// The text box displays the host name and IP list
///
Private void GetLanHost ()
{
For (int i = 0; i <255; i ++)
If (LanHost [i, 0]! = "")
{
TextBox1.Text = textBox1.Text + LanHost [i, 1] + ":" + LanHost [i, 0] + "\r\n";
}
}
///
/// listview1 show search host
///
Private void listLanHost ()
{
ListView1.View = View.List;
ListViewItem aa;
For (int i = 0; i <255; i ++)
{
If (LanHost [i, 0]! = "")
{
Aa = new ListViewItem ();
Aa.Text = LanHost [i, 1];
Aa.Tag = LanHost [i, 0];
ListView1.Items.Add (aa);
}
}
}
///
/// progress bar processing thread
///
Private void progressSearch ()
{
//Label1.Text = "The progress bar is only a time estimate, not the actual search progress!";
ProgressBarSearch.Value = 0;
For (int i = 0; i <255; i ++)
{
ProgressBarSearch.Value = progressBarSearch.Value + 1;
Thread.Sleep (100);
}
}
}
}
Regret: Because the search is slow, the actual search progress is not realized.
Do not understand: When implementing text prompts, when inserting at the beginning and end of mouse events
Private void button1_Click (object sender, System.EventArgs e)
{
Lab1.Text = "Start search"; // new insertion
LanSearch ();
Lab1.Text = "End search"; // New insertion
}
When the text prompt is displayed, it cannot always be displayed in time on lab1, but it will display "End Search" after all threads have finished. |
|