里面要写个死循环判断连接状态~只是用控制台写了个例子。实际业务比较复杂要写成windows服务有没有不用死循环的办法能解决的。
static void Main(string[] args)
{
Monitor mo = new Monitor();
mo.OnStateChanged += new Monitor.StateChanged(mo_OnStateChanged);
mo.Run();
Console.ReadKey();
}
static void mo_OnStateChanged(object sender, StateEventArgs e)
{
Console.WriteLine(e.State);
}
internal class Monitor
{
bool finished = false;
public delegate void StateChanged(object sender, StateEventArgs e);
public event StateChanged OnStateChanged;
public void Run()
{
while(!finished)
{
if(finished)
{
break;
}
else
{
StateEventArgs e = new StateEventArgs();
OnStateChanged(this,e);
if (e.State == 1)
{
finished = true;
}
}
}
}
}
internal class StateEventArgs : EventArgs
{
private int state = 0;
public int State
{
get { return state; }
set
{
if (Helper.GetState() != 0)
{
state = 1;
}
}
}
public StateEventArgs(): base()
{
State = this.state;
}
}
这还没写进真正业务逻辑部分的代码~写进去这死循环坑爹啊。。。这循环一次最少给1秒
有没有好点的解决办法啊。
想了解决办法。但是无从入手。有没有办法能得到操作系统 连接状态改变的方法~要能这样我就不用这个事件去做触发了