c# 中获取主机的DNS域名(练习单线程,多线程,线程池)
来源:互联网 发布:sql exists 怎么用 编辑:程序博客网 时间:2024/06/08 14:45
1.项目结构图如下:
2.运行效果如下:
3.核心源代码:
Form1.cs中的源码(设计源码省略,由于单线程,多线程,线程池只有部分源码不同,将其合到一处)
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.Threading;namespace ScanComputer{ public partial class Form1 : Form { public string start;//记录当前IP字符串 public int n;//计数 public DateTime dt1;//当前时间 public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { groupBox1.Enabled = false; button1.Enabled = false; listBox1.Items.Clear(); int ipCount =n= (int)numericUpDown5.Value - (int)numericUpDown4.Value + 1;//计算总IP数量 // 多线程 //Thread[] scanthreads = new Thread[ipCount]; //for (int i = 0; i < ipCount; i++) //{ // start = numericUpDown1.Value + "." + numericUpDown2.Value + "." + numericUpDown3.Value + "." + (numericUpDown4.Value+i); // scan sc = new scan(this); // sc.ip = start; // scanthreads[i] = new Thread(sc.CheckComputer); // scanthreads[i].Name = i.ToString(); // scanthreads[i].Start(); //} //线成池 //scan[] sc = new scan[ipCount]; //dt1 = DateTime.Now;//记下当前时间 //for (int i = 0; i < ipCount; i++) //{ // start = numericUpDown1.Value + "." + numericUpDown2.Value + "." + numericUpDown3.Value + "." + (numericUpDown4.Value + i); // sc[i] = new scan(this); // sc[i].ip = start; // //ThreadPool.QueueUserWorkItem(new WaitCallback(sc[i].CheckComputer)); //} //单线程 scan sc = new scan(this); dt1 = DateTime.Now;//记下当前时间 for (int i = 0; i < ipCount; i++) { start = numericUpDown1.Value + "." + numericUpDown2.Value + "." + numericUpDown3.Value + "." + (numericUpDown4.Value + i); sc.ip = start; object o = null; sc.CheckComputer(o); } } public delegate void GetComputerDnsDelegate(string strIP, string strHostName);//在一个线程中访问另一个线程(此处为main线程)的控件要使用委托 public void addInfotoListbox(string strIP,string hostName) { if (listBox1.InvokeRequired) { GetComputerDnsDelegate get = addInfotoListbox; listBox1.Invoke(get, strIP, hostName); } else { n--; this.listBox1.Items.Add("IP地址----" + strIP + ",Dns域名---" + hostName+" "); if (n == 0)//若n=0说明所有线程已经完成任务,输出总时间 { DateTime dt2 = DateTime.Now; TimeSpan ts = dt2 - dt1; string str=string.Format("总共用了{0:0.00}毫秒" , ts.TotalMilliseconds);//精确到0.00毫秒 this.listBox1.Items.Add(str); this.groupBox1.Enabled = true; button1.Enabled = true; } } } }}
scan.cs源代码
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.Windows.Forms;using System.Threading;namespace ScanComputer{ class scan { Form1 form; public string ip; IPAddress IP; public scan(Form1 form) { this.form = form; } public void CheckComputer(object obj) { try { IP = IPAddress.Parse(ip); } catch { MessageBox.Show("不合法的IP地址!"); } try { string hostname = Dns.GetHostEntry(IP).HostName; form.addInfotoListbox(ip, hostname); } catch { return; } } }}
- c# 中获取主机的DNS域名(练习单线程,多线程,线程池)
- c# 线程池 多线程
- 单线程、多线程的区别
- java的单线程,多线程
- 多核和单核中多线程及单线程的特点
- C# 多线程的自动管理(线程池)
- C# 多线程的自动管理(线程池)
- C# 多线程的自动管理(线程池)
- C# 多线程的自动管理(线程池)
- C# 多线程的自动管理(线程池)
- 【编程优化】单线程、多线程基础(更新中)
- java中单线程和多线程
- 【转】C# 多线程的自动管理(线程池)(很好)
- 解析C#多线程编程中异步多线程的实现及线程池的使用
- 多线程,单线程
- 单线程 多线程
- 单线程,多线程
- 单线程和多线程
- 函数调用的过程
- [转载]两道题(简化的搜索提示系统+随机数发生器)
- limit.c:(.text+0xa5): undefined reference to `sin'
- Android超时机制
- hdu1321
- c# 中获取主机的DNS域名(练习单线程,多线程,线程池)
- How To Recover From Corrupted OCR Disk
- 筛选法求素数算法详解
- ubuntu rar解压缩出现无效的编码的问题
- 不要相信自己,永远做到最谨慎1(笔记)
- CRC编码电路
- PHP上传文件
- BeanUtils 简介
- 第六章 - 图像变换 - 卷积(cvFilter2D)