benshepherd icon

check internet connection on timer

benshepherd | PRO | 11/12/15 06:00:19 PM UTC | 0 ⭐ | 392 👁️ | Never ⏰ | []
C# |

1.21 KB

|

None

|

0 👍

/

0 👎

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;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            Timer timer = new Timer();
            timer.Interval = 10000;
            timer.Enabled = true;
            timer.Tick += timer_Tick;
        }
 
        void timer_Tick(object sender, EventArgs e)
        {
            try //Try and connect to a website
            {
                using (var client = new WebClient())
                {
                    using (var stream = client.OpenRead("http://www.google.com"))
                    {
                        // Connected, do stuff here
                        // sendMail(); 
                    }
                }
            }
            catch (Exception ex)
            {
                //It failed so now we do nothing
            }
        }
    }
}

Comments