winForm自动登录2(检测网络连接状态)

发现一个大问题,断网之后无法读取到网址,于是想通过另一个程序杀了上一个程序,然后产生新的程序解决。这次学习了怎么使用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;
            }
        }

    }
}
编程能力有限,如有不足或是 失误的地方还望不吝赐教

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值