这是代码,我在命令行应用程序中测试了 snmpconn 方法,但它在 Windows 窗体应用程序运行时冻结,我没有理由吗?
public snmpmain()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.button1.Click += new System.EventHandler(this.snmpconn);
}
private void button1_Click(object sender, EventArgs e)
{
//button1.Enabled = false; will disable the button before the event is fired
this.button1.Click += new System.EventHandler(this.snmpconn);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
int port = 162;
// UdpClient listener;
// IPEndPoint groupEP;
byte[] packet = new byte[1024];
int commlength, miblength, datatype, datalength, datastart, Objecttype, Objectlength;
int agent;
int timestamp;
int entrspc;
int specifictrap;
int finallen;
int objectstart;
string objectid;
string test1;
byte[] test2 = new byte[1024];
int temp;
string tempo;
private void snmpconn(object sender, System.EventArgs e)
{
listBox1.Items.Add("Initializing" + port + "...");
this.button1.Click -= new System.EventHandler(this.snmpconn);
UdpClient listener = new UdpClient(port);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, port);
while (true)
{
listBox1.Items.Add("Waiting....");
packet = listener.Receive(ref groupEP);
listBox1.Items.Add("Processing...");
if (packet.Length != 0)
{ // do some work
}
}
}
这适用于命令行应用程序。
你有线索吗。