ขอถามเรื่องการหา IP Address บน LAN ครับ ทำไมหาเจอแต่โปรแกรมช้ามากๆครับ vc#
คือผมทำโปรเจ็กอยู่ตอนนี้ทำการหา IP Address ของแต่ละเครื่องอยู่ครับอ คือเวลาโปรแกรมผมรันมันก็หา IP address ของเครื่องอื่นเจออยู่ครับแต่โปรแกรมจะช้ามากๆๆรอเป็นชาติเลย ทำยังไงให้มันไวๆเหรอครับ อาจารย์ ก็ให้ไปดูเรื่อง Time,Thead มาผมก็หาดูแต่ไม่รู้จะทำยังไงใครรู้ช่วยบอกทีครับว่าทำยังไงโปรแกรมถึงจะ Scan IP Address ได้ไวๆครับ หรือใครพอจะมีวิธีอื่นหรือใช้ตัวอะไรเข้ามาช่วยในการทำงานช่วยบอกที่ครับแล้วตัว progressBar ทำยังไงมันถึงจะวิ่งตามโปรแกรมที่รันอยู่อะครับตอนนี้พอกดแล้วมันก็ขึ้นเต็ม 100% ตลอดเลย พอจบโปรแกรมแล้วก็ยังไงลดลงมาเลย ช่วยที่ครับ
http://upload.mwake.com/v3.php?id=SH/pkMivZTfdS.JPG
อันนี้ 1.คือเวลารันแล้วนานมากๆ กว่าจะหาแต่ละตัวเสร็จ อันที่ 2.จะเป้นตัว progressBar ครับตัว
http://upload.mwake.com/v3.php?id=vp/8h93bftQbi.JPG
รูปนี้เป็นการหา IP Address เจอนะครับแต่กว่าจะรันโปรแกรมเสร็จหมดนานๆมากๆครับ
อันนี้เป้น code ครับ
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Threading; namespace WindowsApplication1 { public partial class FormIP : Form { Thread sercher; public FormIP() { CheckForIllegalCrossThreadCalls = false; InitializeComponent(); hostName.Text = Dns.GetHostName(); } private void label3_Click(object sender, EventArgs e) { } private void btnUse_Click(object sender, EventArgs e) { IPHostEntry ip = Dns.GetHostByName(Dns.GetHostName()); ipFrom.Text = ip.AddressList[0].ToString(); ipTo.Text = ip.AddressList[0].ToString(); hostName.Text = ip.AddressList[0].ToString(); } private void hostName_TextChanged(object sender, EventArgs e) { } private void btnStart_Click(object sender, EventArgs e) { sercher = new Thread(new ThreadStart(Serche)); add.Items.Clear(); try { IPAddress from = IPAddress.Parse(ipFrom.Text); IPAddress to = IPAddress.Parse(ipTo.Text); } catch (FormatException fe) { MessageBox.Show(fe.Message); return; } sercher.Name = "Network searching thread"; sercher.Start(); add.Items.Add("-->> >>> Please wait while processing is done <<< <<--"); Thread.Sleep(1); if (progressBar1.Value == progressBar1.Maximum) { progressBar1.Value = progressBar1.Minimum; } for (int i = progressBar1.Minimum; i <=100 ; i++) { progressBar1.PerformStep(); } } void Add(string m) { add.Items.Add(m); } void Serche() { Add("########## Show IP Address ########## "); Add(" "); Add(" "); int lastF = ipFrom.Text.LastIndexOf("."); int lastT = ipTo.Text.LastIndexOf("."); string frm = ipFrom.Text.Substring(lastF + 1); string tto = ipTo.Text.Substring(lastT + 1); int result = 0; System.Diagnostics.Debug.WriteLine(frm + " " + tto); for (int i = int.Parse(frm);i <= int.Parse(tto); i++) { try { string address = ipTo.Text.Substring(0, lastT + 1); System.Diagnostics.Debug.WriteLine(ipTo.Text.Substring(0, lastT + 1) + i); IPHostEntry he = Dns.GetHostByAddress(address + i); Add(he.HostName+" "+he.AddressList[0]); result += 1; } catch (SocketException) { } catch (Exception) { } } Add("All done search retrieved " + result + " working stations."); } private void btnexit_Click(object sender, EventArgs e) { Close(); } private void ipFrom_TextChanged(object sender, EventArgs e) { } private void progressBar1_Click(object sender, EventArgs e) { } } }
|