日期:2014-05-17 浏览次数:20856 次
public delegate void MyInvoke(string str); private void Form1_Load(object sender, EventArgs e) { Thread t1 = new Thread(GetNetStatus); t1.IsBackground = true; t1.Start(); } private void SetText(string s) { if (textbox1.InvokeRequired) { MyInvoke _myInvoke = new MyInvoke(SetText); this.Invoke(_myInvoke, new object[] { s }); } else { textbox1.Text = s; } } private void GetNetStatus() { System.Timers.Timer t = new System.Timers.Timer(); t.Interval = 1000;//设置时间间隔为100ms t.AutoReset = true;//设置是执行一次(false)还是一直执行(true) t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件; t.Elapsed += new System.Timers.ElapsedEventHandler(IsInternetConnected); //到达时间的时候执行事件 } [DllImport("wininet.dll")] private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue); private void IsInternetConnected(object source, System.Timers.ElapsedEventArgs e) { int i = 0; string message = ""; bool flag = InternetGetConnectedState(out i, 0); if (flag) { message = "网络连接正常"; } else { message = "网络连接中断"; } SetText(message); }