请教如何优化这段代码
我找到1段代码,是有关端口检测的:
private void button1_Click(object sender, EventArgs e)
{
//扫描端口
int iStart=(int)this.numericUpDown1.Value;
int iEnd=(int)this.numericUpDown2.Value;
try
{
this.progressBar1.Minimum=iStart;
this.progressBar1.Maximum=iEnd;
this.richTextBox1.Text="程序开始扫描端口:\n";
this.richTextBox1.Update();
for(int i=iStart ;i<iEnd;i++)
{
this.progressBar1.Value = i;
TcpClient MyTCP = null;
try
{
MyTCP = new TcpClient(this.textBox1.Text, i);
this.richTextBox1.Text += "端口 " + i.ToString() + " 开放!\n";
this.richTextBox1.Update();
}
catch
{
}
}
this.richTextBox1.Text+="扫描端口结束!\n";
}
catch(Exception Err)
{
MessageBox.Show("扫描端口操作发生错误,错误信息是:"+Err.Message,"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
这段代码虽然可以实现检测开放的端口,但运行起来相当的慢,查找1~50以内开放的端口就要用1分钟左右的时间.还请各位高人指点下如何改进这段代码,谢谢