winForm自动登录2(检测网络连接状态)
2017-01-10 16:31
183 查看
发现一个大问题,断网之后无法读取到网址,于是想通过另一个程序杀了上一个程序,然后产生新的程序解决。这次学习了怎么使用Timer控制循环和利用ping检测网络连接状态。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net; using System.Net.NetworkInformation; using System.Diagnostics; using System.Timers; namespace WindowsFormsApplication3 { public partial class 自动登录 : Form { public 自动登录() { InitializeComponent(); System.Timers.Timer t = new System.Timers.Timer(1800000); //实例化Timer类,设置间隔时间为30分钟; t.Elapsed += new System.Timers.ElapsedEventHandler(Form1_Load); //到达时间的时候执行事件; t.AutoReset = true; //设置是执行一次(false)还是一直执行(true); t.Enabled = true; //是否执行System.Timers.Timer.Elapsed事件; } private void Form1_Load(object sender, EventArgs e) { //while (true) { Ping pingSender = new Ping(); PingReply reply = pingSender.Send("www.baidu.com"); if (reply.Status == IPStatus.Success) { //MessageBox.Show("test"); Process[] ps = Process.GetProcesses(); foreach (Process item in ps) { if (item.ProcessName == "WebBrowserCode") { item.Kill(); } } } else Process.Start("C:\\Users\\Desktop\\WebBrowserCode.exe"); } } private void Form1_Resize(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Minimized) { this.Hide(); this.ShowInTaskbar = false; this.notifyIcon1.Visible = true; } } private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) { if (this.WindowState == FormWindowState.Minimized) { this.Show(); this.WindowState = FormWindowState.Normal; notifyIcon1.Visible = false; this.ShowInTaskbar = true; } } } }编程能力有限,如有不足或是失误的地方还望不吝赐教
相关文章推荐
- 实时检测网络状态及是否可以连接Internet
- [原创]Coding4Fun检测你的网络,用C#获取本机TCP、UDP状态及连接(一)
- C#如何检测网络端口连接的状态
- IOS检测网络连接状态
- 如何去检测网络的连接状态(C#)
- C#检测网络的连接状态
- Android 检测网络连接状态
- 如何去检测网络的连接状态
- Android 检测网络连接状态
- IOS检测网络连接状态
- [原创]Coding4Fun检测你的网络,用C#获取本机TCP、UDP状态及连接(二)
- 实时检测网络状态及是否可以连接Internet
- C#检测网络的连接状态
- android的网络是否连接状态检测及具体连接的网络类型
- C#检测当前网络连接状态
- 长ping 命令,检测网络连接状态
- GTK 程序 检测 网线是否连接 本地网络状态 C语言实现
- android-检测网络连接状态 - 随心
- C#如何检测网络端口连接的状态
- IOS检测网络连接状态