多任務網段掃描
--
---使用動態佈局(優點:界面佈局清晰、控件會隨着窗口大小而改變)
<Grid.RowDefinitions>
<RowDefinition Height="1"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="5"></RowDefinition>
</Grid.RowDefinitions>
<GroupBox Header="掃描的IP地址範圍" Grid.Row="0" Grid.RowSpan="3" FontSize="15px" Margin="0,2,0,-63">
<Canvas Height="48" VerticalAlignment="Bottom">
<Label Content="地址前綴:" Width="80" Canvas.Top="11"/>
<TextBox x:Name="tb1" Height="25" Width="124"Canvas.Left="80" Canvas.Top="11" VerticalAlignment="Center" HorizontalAlignment="Center" />
<Label Content="起始值:" Width="63" Canvas.Top="11" Canvas.Left="209"/>
<TextBox Name="tb2" Height="25" Width="77" Canvas.Left="277" Canvas.Top="11" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Label Content="終止值:" Width="58" RenderTransformOrigin="3.888,1.889" Canvas.Left="359" Canvas.Top="11"/>
<TextBox Name="tb3" Height="25" Width="97" Canvas.Left="422" Canvas.Top="11" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Button Name="btn1" Width="75" Content="開始掃描" Canvas.Left="542" Canvas.Top="10" Height="28" Click="btn1_Click"/>
<Button Name="time_get" Width="93" Content="獲取總時間" Click="time_get_Click" Canvas.Left="622" Canvas.Top="10" Height="28"/>
</Canvas>
</GroupBox>
<TextBlock Grid.Row="2" Name="textblock1" Margin="10,74,10,-103" FontSize="30" Width="674" VerticalAlignment="Center" TextAlignment="Center"/>
<GroupBox Header="掃描信息" Grid.Row="2" Margin="0,114,0,-340" FontSize="15px">
<ListBox Margin="0,0,0,10" Name="listbox1">
</ListBox>
</GroupBox>
---
---定義全局變量 所定義的時間變量sumTime必須爲長整形,若定義爲int,因爲掃面的時間可能太大會超出範圍
long sumTime = 0;
DateTime times = DateTime.Now;
---掃描線程的點擊事件
private void btn1_Click(object sender, RoutedEventArgs e)
{
IPAddress ipStart
IPAddress ipStop
listbox1.Items.Clear()
if (int.Parse(tb3.Text) < int.Parse(tb2.Text))
{
MessageBox.Show("提示:終止值必須大於起始值!")
tb2.Text = ""
tb3.Text = ""
}
try
{
textblock1.Text = null
textblock1.Background = Brushes.AliceBlue
ipStart = IPAddress.Parse(tb1.Text + tb2.Text)
ipStop = IPAddress.Parse(tb1.Text + tb3.Text)
}
catch
{
textblock1.Background = Brushes.Red
textblock1.Foreground = Brushes.White
textblock1.Text = "IP地址有錯,請更正!"
}
if (IPAddress.TryParse(tb1.Text + tb2.Text, out ipStart) && IPAddress.TryParse(tb1.Text + tb3.Text, out ipStop))
{
textblock1.Text = null
textblock1.Background = Brushes.AliceBlue
for (int i = int.Parse(tb2.Text)
{
IPAddress ip = IPAddress.Parse(tb1.Text + i.ToString())
//非多線程(若採用多線程則將下面多線程代碼註釋,將此行代碼截除註釋即可)
//多線程
Thread t = new Thread(myMainTask)
t.Start(ip)
}
}
else
{
textblock1.Background = Brushes.Red
textblock1.Foreground = Brushes.White
textblock1.Text = "IP地址有錯,請更正!"
}
}
---線程執行的方法體
private void myMainTask(Object ip)
{
string hostName
DateTime begin = DateTime.Now
Stopwatch stop = new Stopwatch()
stop.Start()
IPAddress ipAddress = (IPAddress)ip
try
{
hostName = Dns.GetHostEntry(ipAddress).HostName
}
catch
{
hostName = "(不在線)"
}
stop.Stop()
DateTime end = DateTime.Now
TimeSpan ts = end - begin
sumTime += stop.ElapsedMilliseconds
listbox1.Dispatcher.Invoke(() => listbox1.Items.Add("掃描地址:" + ipAddress.ToString() + " , 掃描用時:" + ts.TotalMilliseconds + "毫秒 , " + " 主機DNS名稱:" + hostName))
}
---計算總掃描時間的點擊事件
private void time_get_Click(object sender, RoutedEventArgs e)
{
textblock1.Text = "掃描總線程所用總時間爲:" + sumTime.ToString() + "毫秒";
}