您的位置:首页 > 理论基础 > 计算机网络

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;
}
}

}
}
编程能力有限,如有不足或是失误的地方还望不吝赐教
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: