Advertisement
benshepherd

check internet connection on timer

Nov 12th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Net;
  11.  
  12. namespace WindowsFormsApplication1
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         private void Form1_Load(object sender, EventArgs e)
  22.         {
  23.             Timer timer = new Timer();
  24.             timer.Interval = 10000;
  25.             timer.Enabled = true;
  26.             timer.Tick += timer_Tick;
  27.         }
  28.  
  29.         void timer_Tick(object sender, EventArgs e)
  30.         {
  31.             try //Try and connect to a website
  32.             {
  33.                 using (var client = new WebClient())
  34.                 {
  35.                     using (var stream = client.OpenRead("http://www.google.com"))
  36.                     {
  37.                         // Connected, do stuff here
  38.                         // sendMail();
  39.                     }
  40.                 }
  41.             }
  42.             catch (Exception ex)
  43.             {
  44.                 //It failed so now we do nothing
  45.             }
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement