大致情况:电脑、PVR(网络产品)、路由器组成一个小的局域网。路由器动态分配给PVR一个IP,用电脑去ping PVR的IP。 现在是程序运行后单击Button按钮开始ping,button的单击事件如下:
private void button_Click(object sender,EventArgs e)
{
Thread pingTh=null;
if(pingTh!=null)
{
try
{
pingTh.Abort();
}
catch
{
}
finally
{
pingTh=null;
}
}
pingTh=new Thread(Ping); //Ping是具体实现ping PVR的方法。用ICMP数据包实现ping。
PingTh.Start();
}
现在我想修改为程序运行后,插上网线,然后判断PVR是否获取了路由器动态分配的IP,如果已经获取了,程序不用Button按钮触发,直接执行pingTh.Start()。 现在我关键不知道代码应该加在什么地方?Form_Load里面肯定不行。谁能给个思路,谢谢!程序只差这最后一个问题了。
(只剩下这么多的分了)
private void button_Click(object sender,EventArgs e)
{
Thread pingTh=null;
if(pingTh!=null)
{
try
{
pingTh.Abort();
}
catch
{
}
finally
{
pingTh=null;
}
}
pingTh=new Thread(Ping); //Ping是具体实现ping PVR的方法。用ICMP数据包实现ping。
PingTh.Start();
}
现在我想修改为程序运行后,插上网线,然后判断PVR是否获取了路由器动态分配的IP,如果已经获取了,程序不用Button按钮触发,直接执行pingTh.Start()。 现在我关键不知道代码应该加在什么地方?Form_Load里面肯定不行。谁能给个思路,谢谢!程序只差这最后一个问题了。
(只剩下这么多的分了)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
//要写的代码
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
楼主不妨把代码放到这里,试试看.
可以通过timer控件的Tick事件进行检测 ,每一秒或两秒就搜索依次。
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
//要写的代码
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}